2016-06-24 37 views
4

我有兩個圖如下:如何獲得兩個地圖Java之間的區別?

Map<String, Record> sourceRecords; 
Map<String, Record> targetRecords; 

我想拿到鑰匙從每個maps.i.e.的不同

  1. 它顯示了在sourceRecords中可用但在targetRecords中沒有的映射鍵。
  2. 它顯示了在targetRecords中可用但不在sourceRecords中的映射鍵。

我做到了,如下:

Set<String> sourceKeysList = new HashSet<String>(sourceRecords.keySet()); 
Set<String> targetKeysList = new HashSet<String>(targetRecords.keySet()); 

SetView<String> intersection = Sets.intersection(sourceKeysList, targetKeysList); 
Iterator it = intersection.iterator(); 
while (it.hasNext()) { 
    Object object = (Object) it.next(); 
    System.out.println(object.toString()); 
} 

SetView<String> difference = Sets.symmetricDifference(sourceKeysList, targetKeysList); 
ImmutableSet<String> immutableSet = difference.immutableCopy(); 

編輯

if(sourceKeysList.removeAll(targetKeysList)){ 
      //distinct sourceKeys 
      Iterator<String> it1 = sourceKeysList.iterator(); 
      while (it1.hasNext()) { 
       String id = (String) it1.next(); 
       String resultMessage = "This ID exists in source file but not in target file"; 
       System.out.println(resultMessage); 
       values = createMessageRow(id, resultMessage); 
       result.add(values); 
      } 
     } 
     if(targetKeysList.removeAll(sourceKeysList)){ 
      //distinct targetKeys 
      Iterator<String> it1 = targetKeysList.iterator(); 
      while (it1.hasNext()) { 
       String id = (String) it1.next(); 
       String resultMessage = "This ID exists in target file but not in source file"; 
       System.out.println(resultMessage); 
       values = createMessageRow(id, resultMessage); 
       result.add(values); 
      } 
     } 

我能找到共同的密鑰而不是不同的鍵。請幫忙。

+0

您是否在尋找番石榴的'Sets.symmetricDifference(source.keySet(),target.keySet())'? – blubb

回答

3

套件允許您將刪除元素。

如果產生 「幫手」 規定是不是對你的問題(因爲太多的條目;怎麼樣:

Set<String> sources = get a copy of all source entries 
Set<String> targets = get a copy of all source entries 

則:

sources.removeAll(targets) ... leaves only entries in sources that are only in sources, not in target 

sources.retainAll(targets) ... leaves only entries that are in both sets 

你可以從這裏開始工作...

+0

set1.retainAll(set2)將在兩個集合中提供映射關鍵字。但是對於set1.removeAll(set2),它將從set1中刪除set2中的set2中的鍵。我也想爲set2做同樣的事情。 – Robot

+0

什麼可以防止你在set2上進行相反的「去除」操作? – GhostCat

+0

我應用了同樣的,看到編輯的問題,但沒有解決我的問題 – Robot

4

您可以使用複製SetremoveAll

Set<String> difference = new HashSet<String>(sourceKeysList); 
difference.removeAll(targetKeysList); 

The Set Interface

0
  1. 它顯示sourceRecords可用,但不是在targetRecords映射按鍵。

sourceKeysList.removeAll(targetKeysList)

  • 它顯示在targetRecords可用但不是在sourceRecords映射鍵。
  • targetKeysList.removeAll(sourceKeysList)

    +0

    上面的代碼僅打印targetKeyList中的差異,但不打印在sourceKeyList中。按照說明編輯問題,但沒有解決我的問題。 – Robot

    5

    您可以使用GuavaMaps.difference(Map<K, V> left, Map<K, V> right)方法。它返回一個MapDifference對象,這對於讓所有4種映射條目的方法:

    • 同樣出現在左邊和右邊的地圖
    • 只在左側地圖
    • 只有在正確的地圖
    • 鍵存在

      :在這兩個地圖,而值不同

    所以你的情況,也可能只有3行代碼來解決

    MapDifference<String, Record> diff = Maps.difference(sourceRecords, targetRecords); 
    Set<String> keysOnlyInSource = diff.entriesOnlyOnLeft().keySet(); 
    Set<String> keysOnlyInTarget = diff.entriesOnlyOnRight().keySet(); 
    
    +0

    如何獲取嵌套值的差異,例如 {val1:{val2:{val3:1,val3:2}}}和{val1:{val2:{val3:5,val3:2}}},這裏,區別在於val3的值,其層次結構爲val1:va​​l2:val3。 – pjain

    +1

    @PriyalJain應該問自己的問題,檢查[問]。 –

    +0

    我真的不知道這整個計算器是如何工作的:/ 當我問ppl說它不是正確的問題,我應該把它取下來,當我在這裏問你是否建議將它作爲單獨的問題 – pjain