【问题标题】:Hashmap. Find key(s) which have same values between two hashmaps哈希图。查找两个哈希映射之间具有相同值的键
【发布时间】:2020-02-17 08:53:11
【问题描述】:

假设我们有两个哈希图,如下所示:

 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); 

map1 中的“vaibhav”和 map2 中的“krishna”具有相同的值。

我需要从两个地图中找到具有相同值的键。在这种情况下,“vaibhav”和“Krishna”。

谢谢。

【问题讨论】:

  • “我需要”不是问题。请参阅How to Ask
  • 您可以考虑为此示例输入添加输出。
  • 您可以groupBy 值,然后将键映射到集合中。

标签: java algorithm data-structures


【解决方案1】:

按值分组并将键存储在列表中:

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

它将创建:

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

更新

其他方法

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()));

【讨论】:

  • @ManojBanik 时间复杂度为O( n + m),其中n 和'm' 是列表的大小。也可以用其他方式写。
【解决方案2】:

您可以将时间复杂度提高到O(n + m),其中n 是第一个映射的大小,m 是第二个映射的大小。

  • 我们可以通过将values 作为键和keys 作为值来实现这一点。
  • 步骤:
    • 遍历每个地图。
    • 将所有当前映射值存储在一个新映射中,并将具有该值的所有键收集到一个列表中,并将当前值与该列表一起放入新映射中。
    • 现在,遍历任何新地图集合并获取公用键及其各自的打印值。

片段:

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;
}

演示: https://onlinegdb.com/SJdcpbOXU

【讨论】:

    【解决方案3】:

    这可以通过两个复杂度为 n*m 的 for 循环来实现,其中 n.m 是每个映射的大小。

    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);
        }
        }
    

    谢谢。

    【讨论】:

    猜你喜欢
    • 2018-11-13
    • 1970-01-01
    • 2019-05-03
    • 2015-12-26
    • 1970-01-01
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    • 2011-10-04
    相关资源
    最近更新 更多