Forums Register Login

Compare 2 Maps

+Pie Number of slices to send: Send
I am trying to compare 2 Maps (Map<String, ArrayList<String>>) using MapDifference<K,V> from the Guava utility library. Objective is to compare both Maps and place all keys/values that are unique Map One within a new Map and the same for Map 2. Note. Both Maps are likely to have same keys with different values.


The output from code below:


You can see from the line above that the output shows both Maps have the same Key (KeyOne) with different values (with the exception of "One"). I'd like to create two new Maps to hold these values (1 for Map A and 1 for Map B). Where there is a common value such as the value "one" , it will be stored in map A only. Problem is the line diff.enteries is unmodifiable expression, I am not sure how to extract its values to save within a collection/map.

+Pie Number of slices to send: Send
Have you been through the methods of the Map interface? There is a method which combines two Maps. There are also methods to get Sets of entries, keys and values. The Set interface has methods for set difference union and intersection.
You can get a set of keys, iterate it, get the values from Map1 and put them into Map2.

You just need to check that the Sets returned from your Map implement all those methods. Otherwise you would have to copy those Sets into other types of Set which do implement those methods.
Please do not shoot the fish in this barrel. But you can shoot at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 6761 times.
Similar Threads
Clarification regarding HashMap
Reading Complex Map and Collection
1:1 Map class?
Oh my, oh my...
Getting array back from map
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 08:46:10.