Remove key from a Map while iterating over it in Java
This post will discuss how to remove a key from a Map while iterating over it in Java.
It is not allowed to modify a map in Java while iterating over it to avoid non-deterministic behavior at a later stage. For example, the following code example throws a java.util.ConcurrentModificationException since the remove() method of the Map interface is called during iteration.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] args) { Map<String, Integer> map = new HashMap<>(); map.put("C", 1972); map.put("C++", 1980); map.put("Ruby", 1995); map.put("Java", 1995); map.put("JavaScript", 1995); for (String key: map.keySet()) { if (key.startsWith("C")) { map.remove(key); } } System.out.println(map); } } |
Output:
Exception in thread “main” java.util.ConcurrentModificationException
at java.base/java.util.HashMap$HashIterator.nextNode(HashMap.java:1495)
at java.base/java.util.HashMap$KeyIterator.next(HashMap.java:1518)
at Main.main(Main.java:16)
1. Using Iterator.remove() method
It is permitted to modify a set while iterating over it using iterator’s remove() method, instead of Map’s remove() method. The following code demonstrates this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class Main { public static void main(String[] args) { Map<String, Integer> map = new HashMap<>(); map.put("C", 1972); map.put("C++", 1980); map.put("Ruby", 1995); map.put("Java", 1995); map.put("JavaScript", 1995); Iterator<Map.Entry<String, Integer>> it = map.entrySet().iterator(); while (it.hasNext()) { if (it.next().getKey().startsWith("Java")){ it.remove(); } } System.out.println(map); } } |
Output:
{C++=1980, C=1972, Ruby=1995}
2. Using removeIf() method
For Java 8 and above, you can use the removeIf() method with lambda expressions. The removeIf() method removes all elements from the collection which satisfy the provided predicate. The following code demonstrates its usage:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] args) { Map<String, Integer> map = new HashMap<>(); map.put("C", 1972); map.put("C++", 1980); map.put("Ruby", 1995); map.put("Java", 1995); map.put("JavaScript", 1995); map.entrySet().removeIf(entry -> entry.getKey().startsWith("C")); System.out.println(map); } } |
Output:
{Java=1995, JavaScript=1995, Ruby=1995}
You can also call the removeIf() method on the set returned by the keySet() method. Both these method works since the returned set view of keys is backed by the map, and any changes made to the set are reflected in the map as well.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] args) { Map<String, Integer> map = new HashMap<>(); map.put("C", 1972); map.put("C++", 1980); map.put("Ruby", 1995); map.put("Java", 1995); map.put("JavaScript", 1995); map.keySet().removeIf(key -> key.startsWith("C")); System.out.println(map); } } |
Output:
{Java=1995, JavaScript=1995, Ruby=1995}
3. Using removeAll() method
Stream API also provides the removeAll() method that removes all elements associated with the specified keys. Here’s a program to demonstrates the working of the removeAll() method:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.Arrays; import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] args) { Map<String, Integer> map = new HashMap<>(); map.put("C", 1972); map.put("C++", 1980); map.put("Ruby", 1995); map.put("Java", 1995); map.put("JavaScript", 1995); map.keySet().removeAll(Arrays.asList("Java", "JavaScript")); System.out.println(map); } } |
Output:
{C++=1980, C=1972, Ruby=1995}
That’s all about removing a key from a Map while iterating over it in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)