public static Map<String, MapDifference.ValueDifference<String>> mapCompare(HashMap<String, String> hm, HashMap<String,String> hm2){
MapDifference<String, String> diff = Maps.difference(hm, hm2);
Map<String, MapDifference.ValueDifference<String>> entriesDiffering = diff.entriesDiffering();
System.out.println("difrence is -->" +entriesDiffering);
return entriesDiffering;
}
1:abc 2:xyz 3:eedd 4:[]asdfasd
1:abc 2:xyz 3:eedd 4:[]asdfas2d
**Notice the hm2 4th key entry has a different value.
diffrence is -->{1=(abc 2:xyz 3:eedd 4:[]asdfasd, abc 2:xyz 3:eedd 4:[]asdfas2d)}
Note:- the First key is separated with = in first map, while second map first key is missing altogether) not sure reason. but this is not what i am trying to resolve.
4:[]asdfas2d
It works fine if inputs are proper maps with 4 entries, as shown here (I use Splitter#withKeyValueSeparator
to parse string to map:
@Test
public void properlyParsedMaps() {
Map<String, String> map1 = MAP_SPLITTER.split("1:abc 2:xyz 3:eedd 4:[]asdfasd"); // {1=abc, 2=xyz, 3=eedd, 4=[]asdfasd}
Map<String, String> map2 = MAP_SPLITTER.split("1:abc 2:xyz 3:eedd 4:[]asdfas2d"); // {1=abc, 2=xyz, 3=eedd, 4=[]asdfas2d}
mapCompare(map1, map2); // signature slightly changed to accept any `Map`, not just `HashMap`
// difrence is -->{4=([]asdfasd, []asdfas2d)}
}
On the other hand in your example it seems you have maps with one key "1"
and value which is the rest of the string (so probably not parsed as you expected):
@Test
public void reverseEngineeredMapsFromOutput() {
Map<String, String> map1 = ImmutableMap.of("1", "abc 2:xyz 3:eedd 4:[]asdfasd"); // {1=abc 2:xyz 3:eedd 4:[]asdfasd}
Map<String, String> map2 = ImmutableMap.of("1", "abc 2:xyz 3:eedd 4:[]asdfas2d"); // {1=abc 2:xyz 3:eedd 4:[]asdfas2d}
mapCompare(map1, map2);
// difrence is -->{1=(abc 2:xyz 3:eedd 4:[]asdfasd, abc 2:xyz 3:eedd 4:[]asdfas2d)}
}
Please make sure you input arguments contain what you expect.