小编Rob*_*bot的帖子

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

我有两张地图如下:

Map<String, Record> sourceRecords;
Map<String, Record> targetRecords;
Run Code Online (Sandbox Code Playgroud)

我想让每个maps.ie的键都不同

  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();
Run Code Online (Sandbox Code Playgroud)

编辑

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); …
Run Code Online (Sandbox Code Playgroud)

java collections set

12
推荐指数
3
解决办法
1万
查看次数

标签 统计

collections ×1

java ×1

set ×1