1

Lets consider we have two hashmaps as below:

 HashMap<String, Integer> map1  = new HashMap<>(); 
 map1.put("vishal", 10); 
 map1.put("sachin", 30); 
 map1.put("vaibhav", 20); 

 HashMap<String, Integer> map2  = new HashMap<>(); 
 map2.put("Raja", 10); 
 map2.put("John", 30); 
 map2.put("Krishna", 20); 

The "vaibhav" from map1 and "krishna" from map2 have the same values.

I need to find the keys from both the maps, which have the same values. In this case, "vaibhav" and "Krishna".

Thanks.

Naman
  • 27,789
  • 26
  • 218
  • 353
santhosh kumar
  • 1,981
  • 1
  • 9
  • 28

3 Answers3

3

Group by values and store keys in list:

Stream.of(map1.entrySet(), map2.entrySet())
.flatMap(Collection::stream)
.collect(Collectors.groupingBy(
        Map.Entry::getValue,
        Collectors.mapping(
                Map.Entry::getKey,
                Collectors.toList()
        )
));

It will create:

{20=[vaibhav, Krishna], 10=[vishal, Raja], 30=[sachin, John]}

UPDATE

Other approach

Map<Integer, List<String>> collect = new HashMap<>();
map1.entrySet().forEach(e -> collect
        .computeIfAbsent(e.getValue(), k -> new ArrayList<>())
        .add(e.getKey()));
map2.entrySet().forEach(e -> collect
        .computeIfAbsent(e.getValue(), k -> new ArrayList<>())
        .add(e.getKey()));
Community
  • 1
  • 1
lczapski
  • 4,026
  • 3
  • 16
  • 32
  • @ManojBanik the time complexity is `O( n + m)`, where `n` and 'm' are sizes of list. It can be written in other way. – lczapski Feb 18 '20 at 06:01
1

You can improve the time complexity to O(n + m) where n is the size of first map and m is the size of the second map.

  • We can achieve this by making values as keys and keys as values.
  • Steps:
    • Iterate over each map.
    • Store all current map values in a new map and collect all keys who have that value in a list and put the current value with this list in the new map.
    • Now, iterate over any of the new map collections and get the common keys and it's respective values for printing.

Snippet:

private static void showCommonValueKeys(HashMap<String, Integer> map1,HashMap<String, Integer> map2){
    Map<Integer,List<String>> map1Collect = flipKeyValue(map1);
    Map<Integer,List<String>> map2Collect = flipKeyValue(map2);

    for(Map.Entry<Integer,List<String>> m : map1Collect.entrySet()){
        int key = m.getKey();
        if(map2Collect.containsKey(key)){
            System.out.println("For value " + key);
            System.out.println("First map keys: " + m.getValue().toString());
            System.out.println("Second map keys: " + map2Collect.get(key).toString());
            System.out.println();
        }
    }

}

private static  Map<Integer,List<String>> flipKeyValue(HashMap<String, Integer> map){
     Map<Integer,List<String>> mapCollect = new HashMap<>(); 

     for(Map.Entry<String,Integer> m : map.entrySet()){
        String  key = m.getKey();
        int val = m.getValue();
        mapCollect.putIfAbsent(val,new ArrayList<>());
        mapCollect.get(val).add(key);
     }

     return mapCollect;
}

Demo: https://onlinegdb.com/SJdcpbOXU

nice_dev
  • 17,053
  • 2
  • 21
  • 35
0

This can be achieved through two for loops with a complexity of n*m, where n.m is the size of each map.

Map<String, String> map1 = new HashMap<>();
map1.put("santhosh", "1");
map1.put("raja", "2");
map1.put("arun", "3");


Map<String, String> map2 = new HashMap<>();
map2.put("kumar", "1");
map2.put("mani", "1");
map2.put("tony", "3");

for (Map.Entry<String, String> entry1 : map1.entrySet()) {
  String key1 = entry1.getKey();
  String value1 = entry1.getValue();

  for (Map.Entry<String, String> entry2 : map2.entrySet()) {
    String key2 = entry2.getKey();
    String value2 = entry2.getValue();

    if (value1 == value2) {
      System.out.println(key1 + " " + key2);
    }
    }

Thanks.

santhosh kumar
  • 1,981
  • 1
  • 9
  • 28