java 如何获得两个地图Java之间的差异?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38015282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the difference between two maps Java?
提问by Robot
I have two maps as below :
我有两张地图如下:
Map<String, Record> sourceRecords;
Map<String, Record> targetRecords;
I want to get the keys differ from each of the maps.i.e.
我想获得与每个地图不同的键。ie
- It shows mapping keys available in sourceRecords but not in targetRecords.
- It shows mapping keys available in targetRecords but not in sourceRecords.
- 它显示在 sourceRecords 中可用但在 targetRecords 中不可用的映射键。
- 它显示了 targetRecords 中可用但在 sourceRecords 中不可用的映射键。
I did it as below :
我是这样做的:
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();
EDIT
编辑
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);
}
}
I am able to find the common keys but not distinct keys. Please help.
我能够找到通用键,但不能找到不同的键。请帮忙。
采纳答案by GhostCat
Sets allow you to removeelements as well.
集合也允许您删除元素。
If generating "helper" sets is not a problem for you (because of too many entries; what about:
如果生成“助手”集对您来说不是问题(因为条目太多;那么:
Set<String> sources = get a copy of all source entries
Set<String> targets = get a copy of all source entries
then:
然后:
sources.removeAll(targets) ... leaves only entries in sources that are only in sources, not in target
whereas
然而
sources.retainAll(targets) ... leaves only entries that are in both sets
You can work your way from here ...
你可以从这里开始工作......
回答by Philipp Wendler
You can use Guava's Maps.difference(Map<K, V> left, Map<K, V> right)
method. It returns a MapDifference
object, which has methods for getting all four kinds of map entries:
您可以使用Guava的Maps.difference(Map<K, V> left, Map<K, V> right)
方法。它返回一个MapDifference
对象,该对象具有获取所有四种地图条目的方法:
- equally present in left and right map
- only in left map
- only in right map
- key present in both maps, but with different values
- 同样出现在左右地图中
- 只在左地图
- 只有在正确的地图
- 键存在于两个映射中,但具有不同的值
So in your case, it could be solved with only 3 lines of code:
因此,在您的情况下,只需 3 行代码即可解决:
MapDifference<String, Record> diff = Maps.difference(sourceRecords, targetRecords);
Set<String> keysOnlyInSource = diff.entriesOnlyOnLeft().keySet();
Set<String> keysOnlyInTarget = diff.entriesOnlyOnRight().keySet();
回答by Arnaud
You may use a copy Set
and removeAll
:
您可以使用副本Set
和removeAll
:
Set<String> difference = new HashSet<String>(sourceKeysList);
difference.removeAll(targetKeysList);
请参阅设置界面
回答by Daniel Stradowski
- It shows mapping keys available in sourceRecords but not in targetRecords.
- 它显示在 sourceRecords 中可用但在 targetRecords 中不可用的映射键。
sourceKeysList.removeAll(targetKeysList)
sourceKeysList.removeAll(targetKeysList)
- It shows mapping keys available in targetRecords but not in sourceRecords.
- 它显示了 targetRecords 中可用但在 sourceRecords 中不可用的映射键。
targetKeysList.removeAll(sourceKeysList)
targetKeysList.removeAll(sourceKeysList)