fork download
  1. import java.util.HashMap;
  2. import java.util.Map;
  3.  
  4. class Ideone {
  5. public static <K, V> Map<K, V> mapDifference(Map<? extends K, ? extends V> left, Map<? extends K, ? extends V> right) {
  6. Map<K, V> difference = new HashMap<>();
  7. difference.putAll(left);
  8. difference.putAll(right);
  9. difference.entrySet().removeAll(right.entrySet());
  10. return difference;
  11. }
  12.  
  13. public static void main(String[] args) {
  14. Map<String, String> map1 = new HashMap<>();
  15. Map<String, String> map2 = new HashMap<>();
  16.  
  17. map1.put("a", "1");
  18. map1.put("b", "2");
  19. map1.put("key3", "44");
  20.  
  21. map2.put("b", "2");
  22. map2.put("d", "4");
  23. map2.put("third key", "44");
  24.  
  25. System.out.println(mapDifference(map2, map1));
  26. }
  27. }
Success #stdin #stdout 0.04s 4386816KB
stdin
Standard input is empty
stdout
{third key=44, d=4}