Java 8 – How to remove an entry based on the Key in a Map or HashMap ?

In this article, we will discuss how to remove an entry based on the Key in a HashMap using Java 8 Stream

Remove an entry based on Key in a HashMap :

To remove an entry based on the Key,

  • First iterate through entrySet
  • Remove the entry by comparing the Key passed

Here, we will discuss 2 approaches to remove an entry based on the Key from HashMap

  • Using Java 8 Stream
  • Before Java 8

1. Using Java 8 Stream :

  • Check entry set and remove required entry by comparing Key using removeIf() method
  • Now there are 2 possibilities on comparison,
    • If the entry is present comparing the supplied Key, then corresponding entry will be removed from the original Map entries
    • If it is not present, then original Map won’t be affected and all entries will be present

RemoveEntryBasedOnKeyInMapUsingJava8Stream.java

package in.bench.resources.find.entry.map;

import java.util.HashMap;
import java.util.Map;

public class RemoveEntryBasedOnKeyInMapUsingJava8Stream {

	public static void main(String[] args) {

		// 1. creating HashMap object of type <String, Integer>
		Map<String, Integer> countryPopulation = new HashMap<>(); 


		// 1.1 adding key-value pairs to HashMap object
		countryPopulation.put("Pakistan", 220892331);
		countryPopulation.put("Russia", 146748590);
		countryPopulation.put("Brazil", 213728559);
		countryPopulation.put("Indian", 382357386);
		countryPopulation.put("America", 332429717);


		// 1.2 print original Map entries
		System.out.println("1. Original Map Entries :- \n");


		// 1.3 print Map entries to console
		countryPopulation.forEach((key, value) -> System.out.println(
				"Key : " + key  + "\t\t"  + "Value : "  + value
				));



		// 2. find Entry on the basis of Key
		String keyToFind = "Brazil";


		// 2.1 Remove Entry based on Key
		countryPopulation
		.entrySet()
		.removeIf(entry -> entry.getKey().equals(keyToFind));


		// 2.2 print to console
		System.out.println("\n\n2. After removing Map entry with Key '" + keyToFind + "' :- \n");


		// 2.3 Map entries after removing a particular Key
		countryPopulation.forEach((key, value) -> System.out.println(
				"Key : " + key  + "\t\t"  + "Value : "  + value
				));
	}
}

Output :

1. Original Map Entries :- 

Key : Pakistan		Value : 220892331
Key : America		Value : 332429717
Key : Brazil		Value : 213728559
Key : Indian		Value : 382357386
Key : Russia		Value : 146748590


2. After removing Map entry with Key 'Brazil' :- 

Key : Pakistan		Value : 220892331
Key : America		Value : 332429717
Key : Indian		Value : 382357386
Key : Russia		Value : 146748590

2. Before Java 8 :

  • Check Key Set and remove required entry by comparing Key using remove() method
  • Now there are 2 possibilities on comparison,
    • If the key is present, then corresponding entry will be removed from the original Map entries
    • If it is not present, then original Map won’t be affected and all entries will be present

RemoveEntryBasedOnKeyInMap.java

package in.bench.resources.find.entry.map;

import java.util.HashMap;
import java.util.Map;

public class RemoveEntryBasedOnKeyInMap {

	public static void main(String[] args) {

		// 1. creating HashMap object of type <String, Integer>
		Map<String, Integer> countryPopulation = new HashMap<>(); 


		// 1.1 adding key-value pairs to HashMap object
		countryPopulation.put("Pakistan", 220892331);
		countryPopulation.put("Russia", 146748590);
		countryPopulation.put("Brazil", 213728559);
		countryPopulation.put("Indian", 382357386);
		countryPopulation.put("America", 332429717);


		// 1.2 print original Map entries
		System.out.println("1. Original Map Entries :- \n");


		// 1.3 print Map entries to console
		countryPopulation.forEach((key, value) -> System.out.println(
				"Key : " + key  + "\t\t"  + "Value : "  + value
				));



		// 2. find Entry on the basis of Key
		String keyToFind = "Brazil";


		// 2.1 Remove Entry based on Key
		countryPopulation
		.keySet()
		.remove(keyToFind);


		// 2.2 print to console
		System.out.println("\n\n2. After removing Map entry with Key '" + keyToFind + "' :- \n");


		// 2.3 Map entries after removing a particular Key
		countryPopulation.forEach((key, value) -> System.out.println(
				"Key : " + key  + "\t\t"  + "Value : "  + value
				));
	}
}

Output :

1. Original Map Entries :- 

Key : Pakistan		Value : 220892331
Key : America		Value : 332429717
Key : Brazil		Value : 213728559
Key : Indian		Value : 382357386
Key : Russia		Value : 146748590


2. After removing Map entry with Key 'Brazil' :- 

Key : Pakistan		Value : 220892331
Key : America		Value : 332429717
Key : Indian		Value : 382357386
Key : Russia		Value : 146748590

Related Articles :

References :

Happy Coding !!
Happy Learning !!

Java 8 – How to find an entry based on the Value in a Map or HashMap ?
Java 8 – How to find an entry based on the Key in a Map or HashMap ?