The two Maps do not necessarily have the same number of elements and a given map
can have no elements. When we compare the two Maps, if the two Maps are equal then
it is fine. If the two are unequal I want to identify (print) the elements that are in one Map but not the other map.
Map<String,String> map1 = new HashMap<String,String>();
map1.put("A", "B");
map1.put("C","D");
map1.put("E","F");
map1.put("E","G");
Map<String,String> map2 = new HashMap<String,String>();
map2.put("A", "B");
map2.put("C","D");
map2.put("E","F");
map2.put("F","F");
map2.put("G","G");
if (map1.entrySet().containsAll(map2.entrySet())) {
System.out.println("Map2 is a subset of Map1");
}
else { // find map pairs that are in set 1 but not in set 2
// expected result is key = E and value = G
}
if (map2.entrySet().containsAll(map1.entrySet())) {
System.out.println("Map1 is a subset of Map2");
}
else { // find map pairs that are in set 2 but not in set 1
// expected result is key=F, value=F and key=G, value=G
}
//compare each entry of map
for (final String key : map2.keySet()) {
if (map1.containsKey(key)) {
System.out.println("Value:" + map1.get(key));
}
}