2012-05-30 35 views
-3

可能重複:
How to compare two hashmaps in java?如何在java中比較兩個hasmaps,而不管它們的鍵是什麼?

嗨,我有兩個不同的具有己有值,如以下

地圖地圖,

1.=============Employee================= 


Key : 1_10 : Value : 13/04/2012 
Key : 1_11 : Value : 18/04/2012 
Key : 1_12 : Value : 19/04/2012 
Key : 1_14 : Value : 23/04/2012 
Key : 1_13 : Value : 20/04/2012 
Key : 1_16 : Value : 25/04/2012 
Key : 1_1 : Value : 02/04/2012 
Key : 1_15 : Value : 24/04/2012 
Key : 1_18 : Value : 27/04/2012 
Key : 1_3 : Value : 04/04/2012 
Key : 1_17 : Value : 26/04/2012 
Key : 1_2 : Value : 03/04/2012 
Key : 1_5 : Value : 06/04/2012 
Key : 1_19 : Value : 30/04/2012 
Key : 1_4 : Value : 05/04/2012 
Key : 1_7 : Value : 10/04/2012 
Key : 1_6 : Value : 09/04/2012 
Key : 1_9 : Value : 12/04/2012 
Key : 1_8 : Value : 11/04/2012 

2.=============Working day================= 

Key : 27 : Value : 27/4/2012 
Key : 02 : Value : 02/4/2012 
Key : 26 : Value : 26/4/2012 
Key : 19 : Value : 19/4/2012 
Key : 11 : Value : 11/4/2012 
Key : 04 : Value : 04/4/2012 
Key : 30 : Value : 30/4/2012 
Key : 06 : Value : 06/4/2012 
Key : 13 : Value : 13/4/2012 
Key : 09 : Value : 09/4/2012 
Key : 03 : Value : 03/4/2012 
Key : 23 : Value : 23/4/2012 
Key : 20 : Value : 20/4/2012 
Key : 16 : Value : 16/4/2012 
Key : 10 : Value : 10/4/2012 
Key : 18 : Value : 18/4/2012 
Key : 25 : Value : 25/4/2012 
Key : 17 : Value : 17/4/2012 
Key : 12 : Value : 12/4/2012 
Key : 24 : Value : 24/4/2012 
Key : 05 : Value : 05/4/2012 

我只是想比較這兩個哈希映射並給出va在另一張地圖上不存在的地圖。

+1

這個答案應該工作:http://stackoverflow.com/a/10813470/1400768 – nhahtdh

+0

它沒有幫助,所以我改變了問題,並問 – SAR

+4

@Tony這怎麼沒有幫助?另外,不要問同樣的問題兩次。 –

回答

1

假設map1是你的第一張地圖的散列圖,而map2是你第二張地圖的散列圖。然後,

Collection<String> c1 = map1.values(); 
c1.removeAll(map2.values()); // this one removes all the values from c1 which are also in map2. 
Iterator<String> it = c1.iterator(); 
while (it.hasNext()) { 
    System.out.println(it.next()); 
} 

因此,您將得到map1包含哪些map2不包含的值。

+1

這是很多字符只是迭代集合;我們將來會使用foreach。 –

+0

它解決了你的問題嗎? – JackAss

+1

我沒有問題,除了格式化和額外的詳細程度。 –

相關問題