I have two HashMaps, for example HashMapA and HashMapB. What would be an effective way to find the keys that exist in both HashMaps? My current implementation is as follows:
Integer key;
/* create iterator */
Iterator<Map.Entry<Integer, Foo>> it = HashMapA.entrySet().iterator;
/* iterate through HashMapA using iterator*/
while (it.hasNext()) {
key = it.next().getKey();
if (HashMapB.containsKey(key)) {
/* matching key found */
System.out.println("Got one: " + key);
}
}
This seems to work, but looks inefficient. There is something like
Integer keyInBothMaps = HashMapA.containsKeyOf(HashMapB);
?
Simon source
share