fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12.  
  13. // start - create two arraylist which contains map in it.
  14. List<HashMap<String, String>> a1 = new ArrayList<HashMap<String, String>>();
  15. HashMap<String, String> m1 = new HashMap<String, String>();
  16. HashMap<String, String> m2 = new HashMap<String, String>();
  17. HashMap<String, String> m3 = new HashMap<String, String>();
  18. HashMap<String, String> m4 = new HashMap<String, String>();
  19. m1.put("1", "19");
  20. m2.put("2", "22");
  21. m3.put("3", "2");
  22. m4.put("4", "49");
  23. a1.add(m1);
  24. a1.add(m2);
  25. a1.add(m3);
  26. a1.add(m4);
  27. List<HashMap<String, String>> a2 = new ArrayList<HashMap<String, String>>();
  28. HashMap<String, String> m5 = new HashMap<String, String>();
  29. HashMap<String, String> m6 = new HashMap<String, String>();
  30. HashMap<String, String> m7 = new HashMap<String, String>();
  31. HashMap<String, String> m8 = new HashMap<String, String>();
  32. m5.put("1", "12");
  33. m6.put("2", "22");
  34. m7.put("3", "32");
  35. m8.put("5", "52");
  36. a2.add(m5);
  37. a2.add(m6);
  38. a2.add(m7);
  39. a2.add(m8);
  40. // end - create two arraylist which contains map in it.
  41.  
  42. int i = 0, j = 0;
  43.  
  44. // start- get arrayList a1 index entries, fetch map,put all maps into one map to remove
  45. // duplicate keys,doesn't matter value- because they will be removed anyways)
  46. Map<String, String> lOld = new HashMap<String, String>();
  47. for (i = 0; i < a1.size(); i++) {
  48. HashMap<String, String> a1Map = a1.get(i);
  49. lOld.putAll(a1Map);
  50. }
  51. //end
  52.  
  53. // start- get arrayList a2 index entries, fetch map,put all maps into other map to remove
  54. // duplicate keys,doesn't matter value- because they will be removed anyways)
  55.  
  56. HashMap<String, String> lNew = new HashMap<String, String>();
  57. for (j = 0; j < a2.size(); j++) {
  58. HashMap<String, String> a2Map = a2.get(j);
  59. lNew.putAll(a2Map);
  60. }
  61.  
  62. // check if first map keys (set) is in second map keys (set). if yes, add them into a list.
  63. List<String> toRemove = new ArrayList<>();
  64. Set<String> oldKeys = lOld.keySet();
  65. Set<String> newKeys = lNew.keySet();
  66. for (String oldKey : oldKeys) {
  67. if (lNew.containsKey(oldKey)) {
  68. toRemove.add(oldKey);
  69. }
  70. }
  71.  
  72. // remove that list elements from both sets which will remove them from map itself.
  73. oldKeys.removeAll(toRemove);
  74. newKeys.removeAll(toRemove);
  75.  
  76. // print both map
  77. System.out.println("lold map is: " + lOld);
  78. System.out.println("lNew map is: " + lNew);
  79.  
  80. // don't remove elements froms et while iterating. it will give ConcurrentModificationException
  81. }
  82. }
Success #stdin #stdout 0.05s 2184192KB
stdin
Standard input is empty
stdout
lold map is: {4=49}
lNew map is: {5=52}