0

I want to write a method which will take a map as parameter and replace the keys and values of that map by values and keys. I am trying to do it like this:

public class HashMapKeyValueInterchange{
       public static Map<String, String> getMyMap(ConcurrentHashMap<String, String> m){
           Map<String, String> map2 = new HashMap<String, String>();
           for(Entry<String, String> e:m.entrySet()){
               map2.put(e.getValue(), e.getKey());
           }
           return map2;
       }

       public static void main(String[] args) {
           ConcurrentHashMap<String, String> map1 = new ConcurrentHashMap<String, String>();
           map1.put("ajay", "btech");
           map1.put("manas", "mca");
           map1.put("ashu", "mba");
       }    
}

Using this method I can get a new map(map2) with exchanged key and values, but I want map1 to be exchanged

n00begon
  • 3,503
  • 3
  • 29
  • 42

4 Answers4

2

It is availabe, no need to reinvent the wheel, if you use google collection library Guava then you can use BiMap<K,V>.

It is a map that preserves the uniqueness of its values as well as that of its keys. This constraint enables bimaps to support an "inverse view", which is another bimap containing the same entries as this bimap but with reversed keys and values.

Implementation of BiMap are EnumBiMap, EnumHashBiMap, [HashBiMap][2], ImmutableBiMap

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
  • Seema to me like Guava reinvented the wheel here and the, see http://commons.apache.org/proper/commons-collections/javadocs/api-3.2.1/org/apache/commons/collections/BidiMap.html – oddparity Aug 22 '13 at 06:38
0

If the question is if the code works, the answer is yes. Some comments:

  1. If you want your code tested, you need to call getMyMap with map1 or other adequate parameter.
  2. If you want to see any output, you need to write something like System.out.println( getMyMap( map1 ) );
  3. I'd strongly recommend to use Map instead of ConcurrentHashMap as getMyMap's 1st parameter type, so the function is more general an works with other maps.
  4. To make your code even more general, make it:

    public static <K,V> void getMyMap(Map<V,K> output,Map<K,V> input) {
        for(Entry<K,V> e: input.entrySet() ) {
            output.put(e.getValue(), e.getKey());
        }
    }
    

    This will accept a bigger variety of Map's and store the output in any type of Map is passed as the first parameter. Example:

    getMyMap(new TreeMap<String,String>(),map1);
    getMyMap(new HashMap<String,String>(),map1);
    

A final point is that you don't specify a behavior when values are repeated. The assumption above is that either this case does not occur or that any key in the input map is acceptable as value in the output one.

0

Use this code

public static ConcurrentHashMap<String, String> getMyMap(ConcurrentHashMap<String, String> m){
       ConcurrentHashMap<String, String> map2 = new ConcurrentHashMap<String, String>();
       for(Entry<String, String> e:m.entrySet()){
            map2.put(e.getValue(), e.getKey());
       }
       return map2;
}
public static void main(String[] args) {
    ConcurrentHashMap<String, String> map1 = new ConcurrentHashMap<String, String>();
    map1.put("ajay", "btech");
    map1.put("manas", "mca");
    map1.put("ashu", "mba");
    map1 = getMyMap(map1);
}
Prabhaker A
  • 8,317
  • 1
  • 18
  • 24
0

BiMap is a good choice for this kind of operation

A BiMap is a Map that allows you to view the "inverse" BiMap with inverse() ensures that values are unique, making values() a Set

BiMap.put(key, value) will throw an IllegalArgumentException if you attempt to map a key to an already-present value. If you wish to delete any pre-existing entry with the specified value, use BiMap.forcePut(key, value) instead.

BiMap<String, Integer> userId = HashBiMap.create();
String userForId = userId.inverse().get(id);

Look here for more information

boutta
  • 24,189
  • 7
  • 34
  • 49
Dileep
  • 5,362
  • 3
  • 22
  • 38