How to get the difference between two Java maps?

I have two cards as shown below:

Map<String, Record> sourceRecords;
Map<String, Record> targetRecords;

I want the keys to be different from each of the cards. ie

  • It displays the keys available in sourceRecords, but not in targetRecords.
  • It displays the keys available in targetRecords, but not in the original records.

I did this as shown 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 can find shared keys, but not separate keys. Please, help.

+4
source share
4 answers

Kits allow you to delete items .

If the generation of "helper" sets is not a problem for you (due to too many records, but what:

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 make your way from here ...

+3

Guava Maps.difference(Map<K, V> left, Map<K, V> right) . MapDifference, :

  • ,

, :

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

Set removeAll:

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

.

+4
  • , sourceRecords, targetRecords.

sourceKeysList.removeAll(targetKeysList)

  1. , targetRecords, .

targetKeysList.removeAll(sourceKeysList)

0

All Articles