Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the difference between two maps Java?

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.

like image 428
Robot Avatar asked Jun 24 '16 14:06

Robot


People also ask

How do I compare two maps by their keys in Java 8?

If we want to compare hashmaps by keys i.e. two hashmaps will be equals if they have exactly same set of keys, we can use HashMap. keySet() function. It returns all the map keys in HashSet. We can compare the hashset of keys for both maps using Set.

How do you find the equality of two maps?

Using Map.equals() to check if two HashMaps have the same entries.

What is Map <> in Java?

A Map is an object that maps keys to values. A map cannot contain duplicate keys: Each key can map to at most one value. It models the mathematical function abstraction.


2 Answers

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();
like image 101
Philipp Wendler Avatar answered Sep 18 '22 18:09

Philipp Wendler


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

like image 45
GhostCat Avatar answered Sep 18 '22 18:09

GhostCat