93

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

HashMap<Character, String> myHashMap = new HashMap<Character, String>();
myHashMap.put('a', "test one");
myHashMap.put('b', "test two");

and I want to create a new HashMap in which i put the opposites.

HashMap<String, Character> reversedHashMap = new HashMap<String, Character>();
e.g. Keys "test one" & "test two" and values 'a' & 'b'.
Ken
  • 2,859
  • 4
  • 24
  • 26
  • 1
    Simple question, simple answer. Can you tell us where you got stuck? – Jeroen Vannevel Dec 05 '13 at 22:45
  • 2
    First of all you have to be sure that your values are unique. Otherwise, it should be `Map>` – Alexis C. Dec 05 '13 at 22:47
  • well, I'm fairly new to JAVA and wouldn't know where to start... They all are unique, yes – Ken Dec 05 '13 at 22:47
  • 2
    This is not always possible to do without losing data. Imagine you have this: a -> test one; b -> test two; c -> test one (in your first map). What will you get in your second (output) map? Something like this: test one -> a (or c); test two -> b. So 1st map has 3 entries, 2nd map has just 2. – peter.petrov Dec 05 '13 at 22:51

12 Answers12

155

They all are unique, yes

If you're sure that your values are unique you can iterate over the entries of your old map .

Map<String, Character> myNewHashMap = new HashMap<>();
for(Map.Entry<Character, String> entry : myHashMap.entrySet()){
    myNewHashMap.put(entry.getValue(), entry.getKey());
}

Alternatively, you can use a Bi-Directional map like Guava provides and use the inverse() method :

BiMap<Character, String> myBiMap = HashBiMap.create();
myBiMap.put('a', "test one");
myBiMap.put('b', "test two");

BiMap<String, Character> myBiMapInversed = myBiMap.inverse();

As is out, you can also do it this way :

Map<String, Integer> map = new HashMap<>();
map.put("a",1);
map.put("b",2);

Map<Integer, String> mapInversed = 
    map.entrySet()
       .stream()
       .collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey))

Finally, I added my contribution to the proton pack library, which contains utility methods for the Stream API. With that you could do it like this:

Map<Character, String> mapInversed = MapStream.of(map).inverseMapping().collect();
Alexis C.
  • 91,686
  • 21
  • 171
  • 177
  • 3
    Thanks for explanation of Bi-Directional map usage. I didn't know that and it is really useful for me! – Erçin Akçay Aug 23 '14 at 09:39
  • When I use your java-8 method with the collectors, I get the following error on `Map.Entry::getValue` and `getKey`: `Non-static method cannot be referenced from a static context` – Jan Apr 17 '17 at 22:41
  • 1
    Jan, it's because your map is of a different generic type than declared with the the mapInversed variable. – Marcin Apr 18 '17 at 12:31
  • what if values are duplicate ? how do you handle that ? – Chaitanya Uttarwar Nov 23 '17 at 07:01
  • @ChaitanyaUttarwar You'd have to find a merging strategy to map the different keys that are associated with the same value in your original map (you could group them into a list, sum them, take only one key depending on what you want to achieve). – Alexis C. Nov 23 '17 at 10:25
31

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) 
Riju Thomas
  • 331
  • 3
  • 4
28

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())))
mob
  • 567
  • 5
  • 12
3

I wrote a simpler loop that works too (note that all my values are unique):

HashMap<Character, String> myHashMap = new HashMap<Character, String>();
HashMap<String, Character> reversedHashMap = new HashMap<String, Character>();

for (char i : myHashMap.keySet()) {
    reversedHashMap.put(myHashMap.get(i), i);
}
Ken
  • 2,859
  • 4
  • 24
  • 26
2

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);
}
slim
  • 2,545
  • 1
  • 24
  • 38
hichris123
  • 10,145
  • 15
  • 56
  • 70
2

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.

A4L
  • 17,353
  • 6
  • 49
  • 70
2
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.

Mark Hetherington
  • 1,612
  • 14
  • 24
2

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}
Deepak Arora
  • 191
  • 1
  • 2
1

Use forEach introduced in Java 8

Map<Short, String> regularMap = new HashMap<>();
Map<String, Short> inversedMap = new HashMap<>();

regularMap.forEach((key, value) -> inversedMap.put(value, key));
0

For Reversing the Array of Dictionary. (If values are Unique)

private void reverseArrayMap(List<Map<String, String>> list) {
    // reversing the array of dictionary
    List<Map<String, String>> newList = new ArrayList<>();
    Map<String, String> resDic = new HashMap<>();

    for (Map<String, String> map : list) {
        map.forEach((key, value) -> resDic.put(value, key));
        newList.add(resDic);
    }

    System.out.println("Original Array of Dictionary" + list);
    System.out.println("Reversed Array of Dictionary" + newList);
}      
Josef
  • 2,869
  • 2
  • 22
  • 23
0

for reverting the map, in your case:

private void reverseMap(Map<Character, String> map) {
    Map<String, Character> newList = new HashMap<>();
    map.forEach((key, value) -> newList.put(value, key));
    System.out.println(newList);
}

or you can traverse the old hashmap

HashMap<String, Character> newList = new HashMap<String, Character>();
for (String key : list.keySet()){
   newList.put(list.get(key), key);
}
s0xzwasd
  • 2,895
  • 3
  • 17
  • 26
0

Java :
Simple approach, No need for java 8

Map<String,String> map=new HashMap<>();
Map<String,String> mapInv=new HashMap<>();

for (String key : map.keySet()) 
        mapInv.put(map.get(key), key);

Java 8:
forEach() is a new method to iterate the elements. It is defined in Iterable and Stream interface.

Map<String,String> map=new HashMap<>();
Map<String,String> mapInv=new HashMap<>();

map.forEach((key, value) -> mapInv.put(value, key));

Kotlin :

    val map: Map<String, String> = HashMap()
    val mapInv: MutableMap<String?, String> = HashMap()

    for (key in map.keys) mapInv[map[key]] = key
ucMedia
  • 4,105
  • 4
  • 38
  • 46