<分区>
我有两个 HashMap,比如 HashMapA 和 HashMapB。查找存在于两个 HashMap 中的键的有效方法是什么?我当前的实现如下所示:
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);
}
}
这似乎可行,但看起来效率很低。有没有类似的东西
Integer keyInBothMaps = HashMapA.containsKeyOf(HashMapB);
?