Removing elements from a Map in Java

Very short and simple post on removing elements from a Map in Java. We will be focusing on removing multiple elements and ignore the fact you can remove a single element using Map.remove.

The Map below will be used for this post:

There are a few ways to remove elements. You could loop through the code manually and remove them:

This is how you would do it without access to Java 8+. The Iterator is needed to prevent ConcurrentModificationExceptions when removing elements from the Map.

If you do have access to newer versions of Java (8+) then you can choose from the below instead:

removeIf is a method available to Collections. Yes, a Map itself is not a Collection and does not have access to removeIf itself. But, by using: values, keySet or entrySet, a view of the Map‘s contents is returned. This view implements Collection allowing removeIf to be called on it.

The contents returned by values, keySet and entrySet are very important. Below is an extract of the JavaDoc for values:

* Returns a {@link Collection} view of the values contained in this map.
* The collection is backed by the map, so changes to the map are
* reflected in the collection, and vice-versa.
* The collection supports element removal, which removes the corresponding
* mapping from the map, via the {@code Iterator.remove},
* {@code Collection.remove}, {@code removeAll},
* {@code retainAll} and {@code clear} operations.

This JavaDoc explains that the Collection returned by values is backed by the Map and that changing either the Collection or the Map will alter the other. I don’t think I can explain what the JavaDoc is saying any better than what is already written there… So I’ll stop trying on that part now. I have only shown the documentation for values, but you can trust me when I say that keySet and entrySet are also both backed by the Map‘s contents. You can read the docs yourself if you don’t believe me.

This also links back to the first example using an older Java version. The documentation specifies that Iterator.remove can be used. This is what is used earlier. Furthermore, the implementation of removeIf is very similar to the Iterator example. After talking about it, I might as well show it:

There is a bit extra to it. But, otherwise it is pretty much the same.

So, that’s that. Not much to conclude other than me telling you to remember that using: values, keySet, or entrySet will provide access to removeIf allowing easy removal of Map entries.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: