/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
{
// start - create two arraylist which contains map in it.
List
<HashMap
<String, String
>> a1
= new ArrayList
<HashMap
<String, String
>>(); m1.put("1", "19");
m2.put("2", "22");
m3.put("3", "2");
m4.put("4", "49");
a1.add(m1);
a1.add(m2);
a1.add(m3);
a1.add(m4);
List
<HashMap
<String, String
>> a2
= new ArrayList
<HashMap
<String, String
>>(); m5.put("1", "12");
m6.put("2", "22");
m7.put("3", "32");
m8.put("5", "52");
a2.add(m5);
a2.add(m6);
a2.add(m7);
a2.add(m8);
// end - create two arraylist which contains map in it.
int i = 0, j = 0;
// start- get arrayList a1 index entries, fetch map,put all maps into one map to remove
// duplicate keys,doesn't matter value- because they will be removed anyways)
for (i = 0; i < a1.size(); i++) {
HashMap
<String, String
> a1Map
= a1.
get(i
); lOld.putAll(a1Map);
}
//end
// start- get arrayList a2 index entries, fetch map,put all maps into other map to remove
// duplicate keys,doesn't matter value- because they will be removed anyways)
HashMap
<String, String
> lNew
= new HashMap
<String, String
>(); for (j = 0; j < a2.size(); j++) {
HashMap
<String, String
> a2Map
= a2.
get(j
); lNew.putAll(a2Map);
}
// check if first map keys (set) is in second map keys (set). if yes, add them into a list.
List<String> toRemove = new ArrayList<>();
Set<String> oldKeys = lOld.keySet();
Set<String> newKeys = lNew.keySet();
for (String oldKey
: oldKeys
) { if (lNew.containsKey(oldKey)) {
toRemove.add(oldKey);
}
}
// remove that list elements from both sets which will remove them from map itself.
oldKeys.removeAll(toRemove);
newKeys.removeAll(toRemove);
// print both map
System.
out.
println("lold map is: " + lOld
); System.
out.
println("lNew map is: " + lNew
);
// don't remove elements froms et while iterating. it will give ConcurrentModificationException
}
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uJ3QgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5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