Reverse HashMap keys and values in Java

前端 未结 8 1702
梦谈多话
梦谈多话 2020-11-27 17:59

It\'s a simple question, I have a simple HashMap of which i want to reverse the keys and values.

HashMap myHashMap = new HashMap<         


        
相关标签:
8条回答
  • 2020-11-27 18:08

    Tested with below sample snippet, tried with MapUtils, and Java8 Stream feature. It worked with both cases.

    public static void main(String[] args) {
        Map<String, String> test = new HashMap<String, String>();
        test.put("a", "1");
        test.put("d", "1");
        test.put("b", "2");
        test.put("c", "3");
        test.put("d", "4");
        test.put("d", "41");
    
        System.out.println(test);
    
        Map<String, String> test1 = MapUtils.invertMap(test);
    
        System.out.println(test1);
    
        Map<String, String> mapInversed = 
                test.entrySet()
                   .stream()
                   .collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
    
        System.out.println(mapInversed);
    }
    
    Output:
    {a=1, b=2, c=3, d=41}
    {1=a, 2=b, 3=c, 41=d}
    {1=a, 2=b, 3=c, 41=d}
    
    0 讨论(0)
  • 2020-11-27 18:12
    private <A, B> Map<B, A> invertMap(Map<A, B> map) {
        Map<B, A> reverseMap = new HashMap<>();
        for (Map.Entry<A, B> entry : map.entrySet()) {
            reverseMap.put(entry.getValue(), entry.getKey());
        }
        return reverseMap;
    }
    

    It's important to remember that put replaces the value when called with the same key. So if you map has two keys with the same value only one of them will exist in the inverted map.

    0 讨论(0)
  • 2020-11-27 18:14

    If the values are not unique, the safe way to inverse the map is by using java 8's groupingBy function

    Map<String, Integer> map = new HashMap<>();
    map.put("a",1);
    map.put("b",2);
    
    Map<Integer, List<String>> mapInversed = 
    map.entrySet()
       .stream()
       .collect(Collectors.groupingBy(Map.Entry::getValue, Collectors.mapping(Map.Entry::getKey, Collectors.toList())))
    
    0 讨论(0)
  • 2020-11-27 18:14

    To answer your question on how you can do it, you could get the entrySet from your map and then just put into the new map by using getValue as key and getKey as value.

    But remember that keys in a Map are unique, which means if you have one value with two different key in your original map, only the second key (in iteration order) will be kep as value in the new map.

    0 讨论(0)
  • 2020-11-27 18:18

    Iterate through the list of keys and values, then add them.

    HashMap<String, Character> reversedHashMap = new HashMap<String, Character>();
    for (String key : myHashMap.keySet()){
        reversedHashMap.put(myHashMap.get(key), key);
    }
    
    0 讨论(0)
  • 2020-11-27 18:19

    Apache commons collections library provides a utility method for inversing the map. You can use this if you are sure that the values of myHashMap are unique

    org.apache.commons.collections.MapUtils.invertMap(java.util.Map map)
    

    Sample code

    HashMap<String, Character> reversedHashMap = MapUtils.invertMap(myHashMap) 
    
    0 讨论(0)
提交回复
热议问题