Java 为什么组合两个hashMap的结果不是corecct?

Java 为什么组合两个hashMap的结果不是corecct?,java,map,hashmap,Java,Map,Hashmap,我有两个HashMap,第一个有3149条记录,第二个有5440条记录,当我组合它们时,结果大小小于3149+5440。为什么以及如何解决它 Map<String,String> bigMap = new HashMap<String, String>(); bigMap.putAll(hashMap1); bigMap.putAll(hashMap2); int j = 0; for (Map.Entry<String, String> entry : b

我有两个
HashMap
,第一个有3149条记录,第二个有5440条记录,当我组合它们时,结果大小小于3149+5440。为什么以及如何解决它

Map<String,String> bigMap = new HashMap<String, String>();
bigMap.putAll(hashMap1);
bigMap.putAll(hashMap2);

int j = 0;
for (Map.Entry<String, String> entry : bigMap.entrySet()) {
  System.out.println(j++);
}
Map bigMap=newhashmap();
putAll(hashMap1);
putAll(hashMap2);
int j=0;
对于(Map.Entry:bigMap.entrySet()){
System.out.println(j++);
}
我还检查了这段代码,以确定是否有一些公共密钥。

for (Map.Entry<String, String> entry : readCsv(hashMap1).entrySet()) {
    String key = entry.getKey(); 
    String value = entry.getValue();  
    if(entry.getKey().equals(hashMap2).get(key))){
        System.out.println(i++);
    } 
}
for(Map.Entry:readCsv(hashMap1.entrySet()){
String key=entry.getKey();
字符串值=entry.getValue();
if(entry.getKey().equals(hashMap2.get(key))){
System.out.println(i++);
} 
}

您的
hashMap1
hashMap
可能有许多相同的键。这就是为什么有些条目会被具有类似键的其他条目覆盖。

如果地图中有相同的键,则这是意料之中的。键在地图中必须是唯一的。如果将一个值与一个已经存在的键一起放入映射,那么现有值将被覆盖。

要查找常用键,可以执行以下操作

Set<String> common = new HsahSet<String>(hashMap1.keySet());
common.retainAll(hashMap2.keySet());
System.out.println("Common Keys " + common);
Set common=new HsahSet(hashMap1.keySet());
common.retainal(hashMap2.keySet());
System.out.println(“公用键”+公用键);

如果没有剩下的代码,很难说清楚。两张地图中的记录计数都是正值吗?小于3149+5440,还是小于3149?如果
hashMap1
hashMap2
共享键,则只有这些键的
hashMap2
值会出现在
bigMap
中。可能是因为hashMap1和hashMap2有一些公共键?您确定所有键都是唯一的吗?基本上,你在做一个集合并集,结果是一个基数小于3149+5440的集合,我用这个代码检查是否有一些公共键。
for(Map.Entry条目:readCsv(hashMap1.entrySet()){String key=Entry.getKey();String value=Entry.getValue();if(entry.getKey().equals(hashMap2.get(key)){System.out.println(i++);}