根據不同的按鍵會相同,顯示不同的值的關鍵,你可以嘗試這樣的
public class test {
public static void main(String args[]){
HashMap<String, String> map1 = new HashMap<>();
HashMap<String,String> map2 = new HashMap<>();
map1.put("Name", "Jerry");
map1.put("State", "Texas");
map2.put("Name","TOM");
map2.put("State", "Texas");
Iterator<?> it = map1.entrySet().iterator();
Iterator<?> it1 = map2.entrySet().iterator();
while (it.hasNext() && it1.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
Map.Entry pair1 = (Map.Entry)it1.next();
if(!pair.getValue().equals(pair1.getValue())) {
System.out.println(pair.getKey()+" "+pair.getValue()+" "+pair1.getValue()+" mismatched");
}
}
}
}
輸出一些東西
Name Jerry TOM mismatched
你有問題嗎?如果您嘗試過這樣並且無法正常工作,請發佈您嘗試的內容。如果您還沒有嘗試過,請先嚐試。 – ajb
由於缺乏嘗試/以前的研究而陷入低估。這是非常基本的東西,而且這些東西有很多例子。除此之外:閱讀有關原始類型 - 然後停止使用它們。您應該在地圖聲明的左側放置類型信息! – GhostCat
有什麼問題?獲取關鍵值的值如果不相等則比較值然後打印鍵。 – Lokesh