How to get the difference between two maps Java?

2019-02-16 15:42发布

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.

4条回答
放荡不羁爱自由
2楼-- · 2019-02-16 15:59

You may use a copy Set and removeAll :

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

See The Set Interface

查看更多
混吃等死
3楼-- · 2019-02-16 16:04

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();
查看更多
可以哭但决不认输i
4楼-- · 2019-02-16 16:06
  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)

查看更多
Fickle 薄情
5楼-- · 2019-02-16 16:19

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 ...

查看更多
登录 后发表回答