1. Introduction
While programming you may come across a requirement when you want to remove a key value pair while iterating over a HashMap
. The requirement is to search a key in HashMap
and if found, remove the key value pair.
The way you may think of is to iterate over the HashMap
and compare each value, if found, remove the key value pair using the remove(Object key, Object value)
method. The approach is right but the method to remove the key value pair is wrong. While iterating the HashMap
, if you use remove()
method of java.util.Map
, it will throw ConcurrentModficationException
.
We’ll discuss following ways to remove key value pair from a HashMap
while iterating:
- Using
remove()
method ofIterator
- Using Java 8 lambda expression –
removeIf()
method
2. Using remove() method of iterator
- Get the
HashMap
and the key to be removed. - Create an iterator over the
HashMap
usingiterate()
method. - Iterate over the
HashMap
using theIterator.hasNext()
method. - While iterating, compare the key at that iteration with the key you are searching.
- If a match is found, remove the entry using
remove()
method.
import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class IteratorRemoveExample { public static void main(String[] args) { // Create a HashMap HashMap<Integer, String> map = new HashMap<>(); // put values in HashMap map.put(1, "one"); map.put(2, "two"); map.put(3, "three"); // Get the key to be removed int keyToBeRemoved = 2; // Print the initial HashMap System.out.println("Initial HashMap: " + map); // Get the iterator over the HashMap Iterator<Map.Entry<Integer, String>> iterator = map.entrySet().iterator(); // Iterate over the HashMap while (iterator.hasNext()) { // Get the entry Map.Entry<Integer, String> entry = iterator.next(); // compare the key with the specified key if (keyToBeRemoved == entry.getKey()) { // Remove this entry from HashMap iterator.remove(); } } // Print the modified HashMap System.out.println("Modified HashMap: " + map); } }
Output
Initial HashMap: {1=one, 2=two, 3=three} Modified HashMap: {1=one, 3=three}
3. Using Java 8 lambda expressions – removeIf() method
- Get the
HashMap
and the key to be removed. - Get the entry set of this map using
HashMap.entrySet()
method. - Using lambda expression, remove the entry from the map if the key is equal to the specified key.
import java.util.HashMap; public class RemoveIfExample { public static void main(String[] args) { // Create a HashMap HashMap<Integer, String> map = new HashMap<>(); // put values in HashMap map.put(1, "one"); map.put(2, "two"); map.put(3, "three"); // Print the initial HashMap System.out.println("Initial HashMap: " + map); // Get the key to be removed int keyToBeRemoved = 2; // Remove the specified entry from the Map map.entrySet().removeIf(entry -> (keyToBeRemoved == entry.getKey())); // Print the modified HashMap System.out.println("Modified HashMap: " + map); } }
Output
Initial HashMap: {1=one, 2=two, 3=three} Modified HashMap: {1=one, 3=three}