Java HashMap remove entry examples
There are various ways of removing an item from a Map
type in Java. We will use HashMap
to show various ways of removing an item from a Map
and same can be used for other Map
implementations.
1) Remove an entry from HashMap by using Collection removeIf() method
You can remove an entry from HashMap
by using Collection removeIf()
method which was added in Java 8. It has following signature default boolean removeIf(Predicate filter)
and removes all entries from the Collection
which match the predicate. removeIf()
method will be called on Map.Entry Set
. It uses Iterator remove()
internally and hence doesn't throw ConcurrentModificationException
.
In the example below we first pass a predicte (e -> e.getKey().equals("Mark")
) that checks for the matching key and another predicate (e -> e.getValue().equals(6)
) that checks for the matching value for entry that we want to remove from the HashMap
.
package java8; import java.util.HashMap; import java.util.Map; public class RemoveHashMapEntry { public static void main(String[] args) { Map<String, Integer> map = new HashMap<>(); map.put("John", 2); map.put("Mark", 1); map.put("Thomas", 4); map.put("James", 3); map.put("Edwin", 5); map.put("Vinod", 6); System.out.println(map); map.entrySet().removeIf(e -> e.getKey().equals("Mark")); map.entrySet().removeIf(e -> e.getValue().equals(6)); System.out.println(map); } }
Output :
{Thomas=4, James=3, Edwin=5, John=2, Mark=1, Vinod=6} {Thomas=4, James=3, Edwin=5, John=2}
2) Remove an entry from HashMap by using HashMap remove() method
You can remove an entry from HashMap by using HashMap remove()
method. It has following signature public remove(Object key)
and removes the entry from HashMap for the key specified if the key is present. It returns the previous value associated with key passed.
import java.util.HashMap; import java.util.Map; // Removes entry from HashMap public class MyClass { public static void main(String[] args) { Map<String, String> map = new HashMap<String, String>(); map.put("key1", "value1"); map.put("key2", "value2"); map.put("key3", "value3"); map.remove("key2"); // Remove entry from HashMap System.out.println("map " + map); } }
Output :
map {key1=value1, key3=value3}
3) Remove an entry from HashMap by using Iterator remove() method
You can remove an entry from HashMap
by using Iterator remove()
method. It has following signature default void remove()
. It removes the last element returned by the iterator from the specified collection and can be used only once for one Iterator.next()
call.
You can get Map.Entry Iterator
by calling iterator()
method on Map entrySet()
method. Also this is a preferred approach to remove entry from HashMap
as with this approach you will not get ConcurrentModificationException
.
import java.util.HashMap; import java.util.Iterator; import java.util.Map; // Removes entry from HashMap public class MyClass { public static void main(String[] args) { Map<String, String> map = new HashMap<String, String>(); map.put("key1", "value1"); map.put("key2", "value2"); map.put("key3", "value3"); Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator(); while (iterator.hasNext()){ Map.Entry<String, String> entry = iterator.next(); if (entry.getKey().equalsIgnoreCase("key2")) { iterator.remove(); // Remove entry from HashMap } } System.out.println("map " + map); } }
Output :
map {key1=value1, key3=value3}
If you remove an entry from HashMap
while iterating using HashMap remove()
method, you will get ConcurrentModfiicationException
.
import java.util.HashMap; import java.util.Iterator; import java.util.Map; // Removes entry from HashMap public class MyClass { public static void main(String[] args) { Map<String, String> map = new HashMap<String, String>(); map.put("key1", "value1"); map.put("key2", "value2"); map.put("key3", "value3"); Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator(); while(iterator.hasNext()){ Map.Entry<String, String> entry = iterator.next(); if ("key2".equals(entry.getKey())) { map.remove("key2"); // Remove entry from HashMap } } System.out.println("map " + map); } }
Output :
Exception in thread "main" java.util.ConcurrentModificationException at java.util.HashMap$HashIterator.nextNode(Unknown Source) at java.util.HashMap$EntryIterator.next(Unknown Source) at java.util.HashMap$EntryIterator.next(Unknown Source) at MyClass.main(MyClass.java:16)
4) Remove all the entries from HashMap by using HashMap clear()
You can remove all the entries from HashMap by using HashMap clear()
method. It has following signature public void clear()
. It removes all of the entries from the map.
import java.util.HashMap; import java.util.Map; // Removes all the entries from HashMap public class MyClass { public static void main(String[] args) { Map<String, String> map = new HashMap<String, String>(); map.put("key1", "value1"); map.put("key2", "value2"); map.put("key3", "value3"); map.clear(); System.out.println("map " + map); } }
Output :
map {}
References :
Oracle Docs HashMap remove()
Oracle Docs Iterator remove()
Oracle Docs HashMap clear()