Search code examples
javahashmapkeymultimap

combine key value as a new value for another map with duplicates in java


i am having map1 as <k1,v1> and i have to create map2 with map1 as a value like map2=<k3,map1>.

But keys k1 ans k3 are having duplicate and we have to retain duplictes.

Example:

map1={(1,a),(1,b),(2,c)}
map2={5={1,a},5={1,b},6={2,c}}

How to achieve this using hashmaps or maps(without using guava's multimap concept)


Solution

  • As HashMap doesn't allow to store duplicate keys, you might want to consider changing your code a bit and create HashMap<Key,ArrayList<HashMap>>. Maps with the same key would be stored under the same Key in the ArrayList of your HashMaps. "ArrayList of HashMaps" would be the value of parent HashMap. There is a simple example how I see you could achieve something similar to containing more values to duplicate keys (I used hardcoded key values to make it a bit simpler to read, I also added some explanations in the comments to the code):

    import java.util.*;
    
    public class A {
        public static void main(String[] args) {
            Map<Integer, ArrayList<Character>> map1 = new HashMap<>(); // Map containing few Characters under one key
            map1.put(1, new ArrayList<Character>()); 
            map1.get(1).add('a');
            map1.get(1).add('b');
            map1.put(2, new ArrayList<Character>());
            map1.get(2).add('c');
            System.out.println("map1: " + map1); // prints: map1: {1=[a, b], 2=[c]}
            Map<Integer, ArrayList<HashMap<Integer, Character>>> map2 = new HashMap<>(); // Map storing as keys 5 and 6, values are maps from map1
            map2.put(5, new ArrayList<HashMap<Integer, Character>>());
            map2.put(6, new ArrayList<HashMap<Integer, Character>>());
            for(Map.Entry<Integer, ArrayList<Character>> entry : map1.entrySet()) { // For each Integer-ArrayList pair from map1...
                if(entry.getKey().equals(1)) { // Check if key Integer is equal to 1, if it is...
                    for(Character character : entry.getValue()) { // Create Maps containg pairs of Integer as key and each Character as value...
                        HashMap<Integer, Character> innerMap = new HashMap<>();
                        innerMap.put(entry.getKey(), character);
                        map2.get(5).add((new HashMap<Integer,Character>(innerMap)));
                    }
                }
                if(entry.getKey().equals(2)) { // Check if key Integer is equal to 1, if it is...
                    for(Character character : entry.getValue()) { // Create Maps containg pairs of Integer as key and each Character as value...
                        HashMap<Integer, Character> innerMap = new HashMap<>();
                        innerMap.put(entry.getKey(), character);
                        map2.get(6).add((new HashMap<Integer,Character>(innerMap)));
                    }
                }
    
            }
            System.out.println("map2: " + map2); // prints: map2: {5=[{1=a}, {1=b}], 6=[{2=c}]}
        }
    }
    

    Output you get:

    map1: {1=[a, b], 2=[c]}
    map2: {5=[{1=a}, {1=b}], 6=[{2=c}]}
    

    You can use result Map and combination of getKey() and getValue() methods to create Maps like 5={1,a} and 5=[{1=b} on-the-go (but be aware that you cannot store them in the same Map).