I have a Map
with large number of key values pairs. Now I want to remove selected keys from that Map
. Following code shows wha
Just for the sake of completeness:
As guessed java.util.AbstractSet#removeAll
really iterates over all entries, but with one little trick: It uses the iterator of the smaller collection:
if (size() <= collection.size()) {
Iterator> it = iterator();
while (it.hasNext()) {
if (collection.contains(it.next())) {
it.remove();
}
}
} else {
Iterator> it = collection.iterator();
while (it.hasNext()) {
remove(it.next());
}
}