How to get the difference between two maps Java?

微笑、不失礼 提交于 2019-12-30 08:05:45

问题


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.

  1. It shows mapping keys available in sourceRecords but not in targetRecords.
  2. It shows mapping keys available in targetRecords but not in 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.


回答1:


Sets allow you to remove elements 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 ...




回答2:


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:

  • 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:

MapDifference<String, Record> diff = Maps.difference(sourceRecords, targetRecords);
Set<String> keysOnlyInSource = diff.entriesOnlyOnLeft().keySet();
Set<String> keysOnlyInTarget = diff.entriesOnlyOnRight().keySet();



回答3:


You may use a copy Set and removeAll :

Set<String> difference = new HashSet<String>(sourceKeysList);
difference.removeAll(targetKeysList);

See The Set Interface




回答4:


  1. It shows mapping keys available in sourceRecords but not in targetRecords.

sourceKeysList.removeAll(targetKeysList)

  1. It shows mapping keys available in targetRecords but not in sourceRecords.

targetKeysList.removeAll(sourceKeysList)



来源:https://stackoverflow.com/questions/38015282/how-to-get-the-difference-between-two-maps-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!