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.
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.
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.
Using Map.equals() to check if two HashMaps have the same entries.
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.
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:
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();
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 ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With