java 在java中的同一个循环中迭代两个hashmap的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30906498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 17:48:56  来源:igfitidea点击:

What is the best way to iterate two hashmap in same loop in java?

javadata-structurescollections

提问by Thanga

What's the best way to iterate over the below two maps together? I want to compare two maps values which are strings and have to get the keys and values.

将以下两个地图一起迭代的最佳方法是什么?我想比较两个地图值,它们是字符串并且必须获取键和值。

HashMap<String, String> map1;
HashMap<String, String> map2;

回答by Louis Wasserman

There really isn't a better option than

真的没有比这更好的选择了

for (Map.Entry<String, String> entry1 : map1.entrySet() {
  String key = entry1.getKey();
  String value1 = entry1.getValue();
  String value2 = map2.get(key); 
  // do whatever with value1 and value2 
}

回答by dimo414

Depending on what exactly you're trying to do, there are several reasonable options:

根据您究竟要做什么,有几个合理的选择:

  1. Just compare the contents of two maps

    Guavaprovides a Maps.difference()utility that gives you a MapDifferenceinstance letting you inspect exactly what is the same or different between two maps.

  2. Iterate over their entries simultaneously

    If you just want to iterate over the entries in two maps simultaneously, it's no different than iterating over any other Collection. This questiongoes into more detail, but a basic solution would look like this:

    Preconditions.checkState(map1.size() == map2.size());
    Iterator<Entry<String, String>> iter1 = map1.entrySet().iterator();
    Iterator<Entry<String, String>> iter2 = map2.entrySet().iterator();
    while(iter1.hasNext() || iter2.hasNext()) {
      Entry<String, String> e1 = iter1.next();
      Entry<String, String> e2 = iter2.next();
      ...
    }
    

    Note there is no guarantee these entries will be in the same order (and therefore e1.getKey().equals(e2.getKey())may well be false).

  3. Iterate over their keys to pair up their values

    If you need the keys to line up, iterate over the union of both maps' keys:

    for(String key : Sets.union(map1.keySet(), map2.keySet()) {
      // these could be null, if the maps don't share the same keys
      String value1 = map1.get(key);
      String value2 = map2.get(key);
      ...
    }
    
  1. 只需比较两张地图的内容

    Guava提供了一个Maps.difference()实用程序,它为您提供了一个MapDifference实例,让您可以准确地检查两个地图之间的相同或不同之处。

  2. 同时迭代它们的条目

    如果您只想同时迭代两个映射中的条目,这与迭代任何其他Collection. 这个问题更详细,但基本的解决方案如下所示:

    Preconditions.checkState(map1.size() == map2.size());
    Iterator<Entry<String, String>> iter1 = map1.entrySet().iterator();
    Iterator<Entry<String, String>> iter2 = map2.entrySet().iterator();
    while(iter1.hasNext() || iter2.hasNext()) {
      Entry<String, String> e1 = iter1.next();
      Entry<String, String> e2 = iter2.next();
      ...
    }
    

    请注意,不能保证这些条目的顺序相同(因此很e1.getKey().equals(e2.getKey())可能是错误的)。

  3. 迭代它们的键以配对它们的值

    如果您需要将键对齐,请遍历两个映射键的并集:

    for(String key : Sets.union(map1.keySet(), map2.keySet()) {
      // these could be null, if the maps don't share the same keys
      String value1 = map1.get(key);
      String value2 = map2.get(key);
      ...
    }
    

回答by Julian Kolodzey

My case if maps are the same sizes

如果地图大小相同,我的情况

IntStream.range(0, map1.size()).forEach(i -> map1.get(i).equals(map2.get(i));

回答by juggernaut

for (String key : map1.keySet()) {
if (map2.containsKey(key)) {
        //do whatever
    } else{
          // they don't have same values for keys
    }
}