How to swap keys and values in a Map
package com.javanotes2all.java.collectionfw;
import java.util.HashMap;
import java.util.Map;
public class SwapKeysAndValuesinMap {
public static void main(String[] args) {
HashMap map=new HashMap();
map.put("1", "a");
map.put("2", "b");
map.put("3", "c");
map.put("4", "d");
map.put("5", "e");
map.put("6", "f");
map.put("7", "g");
System.out.println("before swap"+map);
map=reverse(map);
System.out.println("after swap"+map);
}
public static HashMap reverse(Map map) {
HashMap rev = new HashMap();
for(Map.Entry entry : map.entrySet())
rev.put(entry.getValue(), entry.getKey());
return rev;
}
}
0 comments:
Post a Comment