Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Compare 2 Maps

 
Tim Jones
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic