Java 比较,;提取两个Hashmap

Java 比较,;提取两个Hashmap,java,hashmap,Java,Hashmap,我使用Scanner读取一个.txt来生成一个Hashmap, 同样的方法是通过读取B.txt来拥有B Hashmap。 这两个hashmap具有“SOME”相同的键,并且希望彼此结合。 如果钥匙相同,则打印“钥匙,值1,值2”。 以下是我迄今为止所做的: public static void main (String[] args) throws FileNotFoundException { Scanner scanner1 = new Scanner(new File("score.txt

我使用Scanner读取一个.txt来生成一个Hashmap, 同样的方法是通过读取B.txt来拥有B Hashmap。 这两个hashmap具有“SOME”相同的键,并且希望彼此结合。 如果钥匙相同,则打印“钥匙,值1,值2”。 以下是我迄今为止所做的:

public static void main (String[] args) throws FileNotFoundException {
Scanner scanner1 = new Scanner(new File("score.txt"));
Map<String, String> tolerance = new HashMap<>();
Scanner scanner2 = new Scanner(new File("Count2.txt"));
Map<String, String> Pdegree = new HashMap<>();
while (scanner1.hasNextLine()) {
String line = scanner1.nextLine();
String[] array = line.split("\t",2);
String Name = array[0];
String score = array[1];
tolerance.put(Name,score);
}
while (scanner2.hasNextLine()) {
    String line2 = scanner2.nextLine();
    String[] array2 = line2.split("\t",2);
    String Name2 = array2[0];
    String degree = array2[1];
    Pdegree.put(Name2,degree);
    }
    for(Map.Entry<String, String> entry : tolerance.entrySet()) {
    String key = entry.getKey();
    String value = entry.getValue();
            for(Map.Entry<String, String> entry2 : Pdegree.entrySet()) {
            String key2 = entry2.getKey();
            String value2 = entry2.getValue();
            if(key==key2){
            System.out.println(key2 + "\t" + value + "\t" + value2);
            }
    }
 }
}
}
publicstaticvoidmain(字符串[]args)抛出FileNotFoundException{
Scanner scanner1=新扫描仪(新文件(“score.txt”);
映射容差=新HashMap();
Scanner scanner2=新扫描仪(新文件(“Count2.txt”);
Map Pdegree=newhashmap();
while(scanner1.hasNextLine()){
String line=scanner1.nextLine();
String[]数组=line.split(“\t”,2);
字符串名称=数组[0];
字符串分数=数组[1];
公差。放置(名称、分数);
}
while(scanner2.hasNextLine()){
字符串line2=scanner2.nextLine();
字符串[]array2=line2.split(“\t”,2);
字符串名称2=array2[0];
串度=array2[1];
Pdegree.put(名称2,度);
}
对于(Map.Entry:tolerance.entrySet()){
String key=entry.getKey();
字符串值=entry.getValue();
对于(Map.Entry entry2:Pdegree.entrySet()){
字符串key2=entry2.getKey();
字符串value2=entry2.getValue();
如果(键==键2){
System.out.println(键2+“\t”+值+“\t”+值2);
}
}
}
}
}
结果和错误消息都不会显示。
我的问题是如何从两个映射中提取具有各自值的相同键。谢谢

您可以使用
map1.putAll(map2)
组合两个地图

为什么不使用番石榴的多重地图?我相信如果你使用put all,它会遇到两个相同的键,它只会给键添加第二个值。然后可以打印出所有的键值对。如果它具有相同的键和相同的值,那么它所做的工作取决于实现


我自己找到了答案。应该是

 if(key.equals(key2))

请在此说明您的问题所在我知道该方法,但我想比较键,因为并非所有键都相同。地图A还有数百把钥匙。