Guest User

Untitled

a guest
Dec 13th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. static boolean containsAllEntries(Map<?,?> map1, Map<?,?> map2) {
  2. return map1.entrySet().containsAll(map2.entrySet());
  3. }
  4.  
  5. static boolean containsAllKeys(Map<?,?> map1, Map<?,?> map2) {
  6. return map1.keySet().containsAll(map2.keySet());
  7. }
  8.  
  9. public static void main(String[] args) {
  10. HashMap<String, String>
  11. map1 = new HashMap<>(), map2 = new HashMap<>();
  12.  
  13. map1.put("1", "Один"); map2.put("1", "Один");
  14. map1.put("2", "Два"); map2.put("2", "Два");
  15. map1.put("3", "Три"); map1.put("3", "Три");
  16.  
  17. map1.put("4", "Четыре");
  18. map1.put("5", "Пять");
  19.  
  20. System.out.println("map1 contains all keys from map2: " + containsAllKeys(map1, map2));
  21. System.out.println("map1 contains all entries from map2: " + containsAllEntries(map1, map2));
  22.  
  23. map1.put("2", "Не два!");
  24. System.out.println("map1 contains all keys from map2: " + containsAllKeys(map1, map2));
  25. System.out.println("map1 contains all entries from map2: " + containsAllEntries(map1, map2));
  26.  
  27. map1.remove("2");
  28. System.out.println("map1 contains all keys from map2: " + containsAllKeys(map1, map2));
  29. System.out.println("map1 contains all entries from map2: " + containsAllEntries(map1, map2));
  30. }
Add Comment
Please, Sign In to add comment