Remove elements from a Map
Java Map provides several ways to remove entries: Map.remove(key) for single removal, remove(key, value) for conditional removal that only deletes if the value also matches, and entrySet().removeIf() for predicate-based bulk removal (Java 8+). This article covers all approaches with practical examples.
MapHashMapLinkedHashMap
Remove Map entries using a Key
To remove an entry from Mapremove(key)
public static void removeEntryUsingKey() {
Map<String, String> map = new HashMap<>(Map.of("Item 1","Tomatoes", "Item 2","Oranges"));
System.out.println("BEFORE , map size : "+map.size() + " , entries " + map);
map.remove("Item 1");
System.out.println("AFTER , map size : "+map.size() + " , entries " + map);
}
In the above example, we are removing an entry with key "Item 1"
AFTER , map size : 1 , entries {Item 2=Oranges}
Remove Map entries using a Key and Value
This method remove(key, value)remove(key)
For example, say if the Map has these entries {{"Item 1","Tomatoes"}, {"Item 2", "Oranges"}}remove(key, value)('Item 1' and 'Apple')Item 1AppleTomatoes
public static void removeEntryUsingKeyAndValue() {
Map<String, String> map = new HashMap<>(Map.of("Item 1","Tomatoes", "Item 2","Oranges"));
System.out.println("BEFORE , map size : "+map.size() + " , entries " + map);
map.remove("Item 1", "Apple");
System.out.println("AFTER , map size : "+map.size() + " , entries " + map);
}
When you run the program, you will see below output. As you see it did not remove the entry with key as 'Item 1'.
AFTER , map size : 2 , entries {Item 2=Oranges, Item 1=Tomatoes}
When you run the below program, it should remove the entry with key and value pair as ('Item 1', 'Tomatoes')
public static void removeEntryUsingKeyAndValue() {
Map<String, String> map = new HashMap<>(Map.of("Item 1","Tomatoes", "Item 2","Oranges"));
System.out.println("BEFORE , map size : "+map.size() + " , entries " + map);
map.remove("Item 1", "Tomatoes");
System.out.println("AFTER , map size : "+map.size() + " , entries " + map);
}
When you run the program, you will see below output.
AFTER , map size : 1 , entries {Item 2=Oranges}
Remove Map entries using Map's EntrySet Iterator
To delete an entry from Map, we can do that using Map's EntrySet
public static void removeEntryUsingIterator() {
Map<String, String> map = new HashMap<>(Map.of("Item 1","Tomatoes", "Item 2","Oranges"));
System.out.println("BEFORE , map size : "+map.size() + " , entries " + map);
Iterator<Map.Entry<String, String>> ite = map.entrySet().iterator();
while(ite.hasNext()) {
if("Item 1".equals(ite.next().getKey())) {
ite.remove();
}
}
System.out.println("AFTER , map size : "+map.size() + " , entries " + map);
}
When you run the program, you will see below output.
AFTER , map size : 1 , entries {Item 2=Oranges}
Remove Map entries using removeIf method on Map's EntrySet or KeySet or Value(s) Collection
You can also remove Map entries using removeIfEntrySetKeySetCollectionremoveIf
You can obtain Map's EntrySetentrySet()removeIfremoveIf
public static void removeUsingRemoveIfOnEntrySet() {
Map<String, String> map = new HashMap<>(Map.of("Item 1","Tomatoes", "Item 2","Oranges"));
System.out.println("BEFORE , map size : "+map.size() + " , entries " + map);
map.entrySet().removeIf(entry-> "Item 1".equals(entry.getKey()));
System.out.println("AFTER , map size : "+map.size() + " , entries " + map);
}
You can obtain Map's KeySet using keySet()
public static void removeUsingRemoveIfOnKeySet() {
Map<String, String> map = new HashMap<>(Map.of("Item 1","Tomatoes", "Item 2","Oranges"));
System.out.println("BEFORE , map size : "+map.size() + " , entries " + map);
map.keySet().removeIf(entry-> "Item 1".equals(entry));
System.out.println("AFTER , map size : "+map.size() + " , entries " + map);
}
You can obtain Map's Values collection using values()
public static void removeUsingRemoveIfOnValues() {
Map<String, String> map = new HashMap<>(Map.of("Item 1","Tomatoes", "Item 2","Oranges"));
System.out.println("BEFORE , map size : "+map.size() + " , entries " + map);
map.values().removeIf(val-> "Tomatoes".equals(val));
System.out.println("AFTER , map size : "+map.size() + " , entries " + map);
}
AFTER , map size : 1 , entries {Item 2=Oranges}