Using a combination of cloning constructors and removeAll() for subsetting…
Map<String, String> beforeMap = new HashMap<String, String>(); beforeMap.put("a", "1"); beforeMap.put("b", "2"); beforeMap.put("c", "3"); Map<String, String> afterMap = new HashMap<String, String>(); afterMap.put("a", "1"); afterMap.put("c", "333");
Code
Set<String> removedKeys = new HashSet<String>(beforeMap.keySet()); removedKeys.removeAll(afterMap.keySet()); Set<String> addedKeys = new HashSet<String>(afterMap.keySet()); addedKeys.removeAll(beforeMap.keySet()); Set<Entry<String, String>> changedEntries = new HashSet<Entry<String, String>>(afterMap.entrySet()); changedEntries.removeAll(beforeMap.entrySet()); System.out.println("added " + addedKeys); System.out.println("removed " + removedKeys); System.out.println("changed " + changedEntries);
Output
added [] removed [b] changed [c=333]