我有兩個圖如下:如何獲得兩個地圖Java之間的區別?
Map<String, Record> sourceRecords;
Map<String, Record> targetRecords;
我想拿到鑰匙從每個maps.i.e.的不同
- 它顯示了在sourceRecords中可用但在targetRecords中沒有的映射鍵。
- 它顯示了在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);
}
}
我能找到共同的密鑰而不是不同的鍵。請幫忙。
您是否在尋找番石榴的'Sets.symmetricDifference(source.keySet(),target.keySet())'? – blubb