如何获得两个映射Java之间的区别?

前端之家收集整理的这篇文章主要介绍了如何获得两个映射Java之间的区别?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有两张地图如下:
Map<String,Record> sourceRecords;
Map<String,Record> targetRecords;

我想让每个map.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);
            }
        }

我能够找到公共密钥但不能找到不同的密钥.请帮忙.

解决方法

集也允许您删除元素.

如果生成“帮助”集对你来说不是问题(因为条目太多;那么:

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

你可以从这里开始工作……

原文链接:https://www.f2er.com/java/127259.html

猜你在找的Java相关文章