Remove a key from a Map in Java
This post will discuss how to remove a key from a Map in Java.
1. Using remove() method
The standard solution is to remove the mapping for a key from a Map in Java is using the remove() method of the Map interface. When the remove() method is called upon a key key, a mapping from key k to value v is removed for which Objects.equals(key, k) holds. This equality holds true for other examples in this post.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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++", 1980); map.put("Java", 1995); map.put("Ruby", 1995); map.remove("C++"); System.out.println(map); } } |
Output:
{Java=1995, Ruby=1995}
You can also call the remove() method on the set view of keys returned by the keySet() method. This works since the returned set is backed by the map, and any changes made to the set are reflected in the map as well. This holds true for the entrySet() method as well.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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++", 1980); map.put("Java", 1995); map.put("Ruby", 1995); map.keySet().remove("C++"); System.out.println(map); } } |
Output:
{Java=1995, Ruby=1995}
2. Using removeIf() method
Since Java 8, you can use the removeIf() method to remove all entries from the map that satisfy the given predicate. The following code demonstrates its usage:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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++", 1980); map.put("Java", 1995); map.put("Ruby", 1995); map.entrySet().removeIf(entry -> entry.getKey().equals("C++")); System.out.println(map); } } |
Output:
{Java=1995, Ruby=1995}
Here’s an equivalent example using the keySet() method:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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++", 1980); map.put("Java", 1995); map.put("Ruby", 1995); map.keySet().removeIf(key -> key.equals("C++")); System.out.println(map); } } |
Output:
{Java=1995, Ruby=1995}
3. Using removeAll() method
A plausible way in Java 8 and above is using the removeAll() method, which removes all mappings associated with the specified keys.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
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++", 1980); map.put("Java", 1995); map.put("Ruby", 1995); map.keySet().removeAll(Arrays.asList("C++", "Java")); System.out.println(map); } } |
Output:
{Ruby=1995}
That’s all about removing a key from a Map 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 :)