如何获得两个映射Java之间的区别?
发布时间:2020-05-26 01:57:21 所属栏目:Java 来源:互联网
导读:我有两张地图如下: MapString, Record sourceRecords;MapString, Record targetRecords; 我想让每个map.i.e的键都不同. 它显示sourceRecords中可用的映射键,但不显示targetRecords中的映射键. 它显示targetRecords中可用的映射键,但不显示sourceRecords中的
|
我有两张地图如下: Map<String,Record> sourceRecords; Map<String,Record> targetRecords; 我想让每个map.i.e的键都不同. >它显示sourceRecords中可用的映射键,但不显示targetRecords中的映射键. 我做了如下: 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);
}
}
我能够找到公共密钥但不能找到不同的密钥.请帮忙. 解决方法集也允许您删除元素.如果生成“帮助”集对你来说不是问题(因为条目太多;那么: 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 你可以从这里开始工作…… (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
