Map
is an interface class in Java and there are several implementations of this,
for example
HashMap
,
LinkedHashMap
and many more.
In below examples we will reference one of the implementation, but the interface methods are mostly supported in all
implementations unless the implementation does not support these methods like if it's an un modifiable Map etc.
Remove Map entries using a Key
Remove Map entries using a Key and Value
Remove Map entries using Map's EntrySet Iterator
Remove Map entries using removeIf method on Map's EntrySet or KeySet or Value(s) Collection
To remove an entry from
Map
using it's key, we can use the
remove(key)
method where
key
is the Key object of that entry. In case if the key doesn't exist in Map, it will not remove anything.
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"
from the Map.
When you run the program, you will see below output.
BEFORE , map size : 2 , entries {Item 2=Oranges, Item 1=Tomatoes}
AFTER , map size : 1 , entries {Item 2=Oranges}
This method
remove(key, value)
is same as
remove(key)
, but it also checks
the value corresponding to the key in Map,
and if the value you are passing is not same as the value from the Map, then it will not remove the entry.
For example, say if the Map has these entries
{{"Item 1","Tomatoes"}, {"Item 2", "Oranges"}}
and
say if you call
remove(key, value)
method using arguments as
('Item 1' and 'Apple')
,
this will not remove the entry with key
Item 1
though it exists, this is because the value you are passing is
Apple
but the corresponding value in Map is
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", "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'.
BEFORE , map size : 2 , entries {Item 2=Oranges, Item 1=Tomatoes}
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.
BEFORE , map size : 2 , entries {Item 2=Oranges, Item 1=Tomatoes}
AFTER , map size : 1 , entries {Item 2=Oranges}
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.
BEFORE , map size : 2 , entries {Item 2=Oranges, Item 1=Tomatoes}
AFTER , map size : 1 , entries {Item 2=Oranges}
You can also remove Map entries using
removeIf
method on Map's
EntrySet
,
KeySet
or values
Collection
. The
removeIf
method internally uses Iterator, so you can also achieve the same result from above method as well.
You can obtain Map's
EntrySet
using
entrySet()
method, then call
removeIf
method
and you can pass Lambda function as Predicate argument.
Below is an example of using EntrySet, when you use
removeIf
on EntrySet, you can both Key and Value pair available at the same time,
so you can use either to determine your condition to remove the entry.
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()
method.
In this case, parameter passed to Predicate is the type of key that the Map is declared with.
Below is an example of 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()
method.
In this case, parameter passed to Predicate is the type of value that the Map is declared with.
Below is an example of using values collection.
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);
BEFORE , map size : 2 , entries {Item 2=Oranges, Item 1=Tomatoes}
AFTER , map size : 1 , entries {Item 2=Oranges}