Home > Back-end >  Convert keys to Values
Convert keys to Values

Time:01-03

I'm trying to convert the keys of a Map to Values of another Map, but finally only one key was return as Value. What was the problem? when the program excuted I got different Result

        import java.util.ArrayList;
        import java.util.HashMap;
        import java.util.Map;

        public class KeystoValues {

            public static void KtoV(Map<Double, Integer> Key) {
                

                Map<Double, Integer> List = new HashMap<Double, Integer>();
                Map<Integer, Double> KeystoV = new HashMap<Integer, Double>();
                
                System.out.println("List Map\n");
                
                List.putAll(Key);
                for(Map.Entry<Double, Integer> val : List.entrySet()) {
                    System.out.println(val.getKey()   ","   val.getValue());
                }
                    
                for(int h = 1; h<=List.size(); h  )
                    for(Map.Entry<Double, Integer> convert : List.entrySet()) {
                        Double j = convert.getKey();
                        KeystoV.put(h, j);
                    }   
                
                System.out.println("\nSet of keys in List Map now converted to set "
                          "of Values and stored to KeystoV Map\n\nKeystoV Map\n");
                
                for(Map.Entry<Integer, Double> converted : KeystoV.entrySet()) {
                    System.out.println(converted.getKey()   ","   converted.getValue());
                }
                
                
            }
            public static void main(String[] args) {
                // TODO Auto-generated method stub
                
                ArrayList<Integer> Value = new ArrayList<Integer>();
                Map<Double, Integer> Key = new HashMap<Double, Integer>();
                
                Key.put(45.0,1);
                Key.put(40.0,2);
                Key.put(23.0,2);
                Key.put(25.0,3);
                Key.put(0.0,1);
                
                KtoV(Key);
                
            }

        }

List Map

                0.0,1
                25.0,3
                40.0,2
                45.0,1
                23.0,2

Set of keys in List Map now converted to set of Values and stored to KeystoV Map

KeystoV Map

                1,23.0
                2,23.0
                3,23.0
                4,23.0
                5,23.0

CodePudding user response:

The problem with your code is this nested for loop:

        for(int h = 1; h<=List.size(); h  )
            for(Map.Entry<Double, Integer> convert : List.entrySet()) {
                Double j = convert.getKey();
                KeystoV.put(h, j);
            }   

If you debug it, then you'll see that you are always putting the last iterated value of List.entrySet() as the value of all keys.

Try changing it to:

int index = 1;
for (Map.Entry<Double, Integer> convert : List.entrySet()) {
    KeystoV.put(index, convert.getKey());
    index  ;
}

CodePudding user response:

You have to use a list for the second map values because some of your values could appear twice and that would result in duplicate keys which maps can't support.

Try it like this.

Map<Double, Integer> map = Map.of(2.0, 1, 3.0, 1, 8.0, 5, 9.0, 7, 4.0, 7);

Map<Integer, List<Double>> keystoV  = map.entrySet().stream().collect(Collectors.groupingBy(Entry::getValue,
        Collectors.mapping(Entry::getKey, Collectors.toList())));

map.entrySet().forEach(System.out::println);
System.out.println();
keystoV.entrySet().forEach(System.out::println);

prints

9.0=7
8.0=5
2.0=1
3.0=1
4.0=7

1=[2.0, 3.0]
5=[8.0]
7=[9.0, 4.0]

Here is a loop version using the Map.computeIfAbsent method.

  • if key is absent, create a list for that key.
  • it also returns the list so the new value (old key) many be added to the list.
keystoV.entrySet().forEach(System.out::println);
Map<Integer, List<Double>> result = new HashMap<>();

for(Entry<Double, Integer> e : map.entrySet()) {
    result.computeIfAbsent(e.getValue(), v->new ArrayList<>()).add(e.getKey());
}

result.entrySet().forEach(System.out::println);

prints

1=[2.0, 3.0]
5=[8.0]
7=[9.0, 4.0]
  • Related