It's not a secret anymore!
The moose likes Java in General and the fly likes Compare 2 Maps Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Compare 2 Maps" Watch "Compare 2 Maps" New topic

Compare 2 Maps

Tim Jones

Joined: Apr 06, 2008
Posts: 28
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.

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46356
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.
I agree. Here's the link:
subject: Compare 2 Maps
It's not a secret anymore!