Why the iterator on map.vaules can be used to remove HashMap#Entry?

Viewed 223

I'm trying to remove all the entry of which the value is null. The code is:

Map<String, String> map = new HashMap<>();
map.put("one", null);
map.put("two", null);
map.put("three", "THREE");

Iterator iterator = map.values().iterator();
while (iterator.hasNext())
{
    if (iterator.next() == null) {
        iterator.remove();
    }
}

for (Map.Entry<String, String> e : map.entrySet()) {
    System.out.println(e.getKey() + ":" + e.getValue());
}

My question is iterator is bind to map.values, why it can remove the whole entry?

2 Answers
Related