Java 8 – How to remove an entry from HashMap by comparing keys ?

In the previous article, we have discussed on how to remove an entry or entries from HashMap by comparing values using Java 8 APIs

Here, we will discuss how can we remove entry/entries from HashMap using Java 8 APIs by comparing keys

Remove entries using removeIf() method by comparing keys :

  • removeIf() – Removes all of the elements of this collection that satisfy the given predicate
  • Syntax: default boolean removeIf(Predicate filter);
  • We will cover different cases to remove entry/entries using removeIf() method by comparing keys using Java 8 APIs

Let’s discuss removing entries from Map using different operators like =, >, <, etc.,

1. Remove an entry by comparing keys using Equal-To (=) operator

RemoveEntryFromHashMapByComparingKeys.java

package in.bench.resources.map.java8.example;

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

public class RemoveEntryFromHashMapByComparingKeys {

	public static void main(String[] args) {

		// create HashMap
		Map<Integer, String> map = new HashMap<Integer, String>();

		// put few items
		map.put(1, "Google");
		map.put(2, "YouTube");
		map.put(3, "Facebook");
		map.put(4, "Yahoo");
		map.put(5, "Amazon");
		map.put(6, "Reddit");
		map.put(7, "Twitter");

		// printing map before removeIf
		System.out.println("Before removing entry from Map : \n" + map);

		// removing an Entry using Java 1.8 (boolean equalTo expression)
		map.entrySet().removeIf(
				ranks -> ranks.getKey().compareTo(Integer.valueOf(4)) == 0);

		// printing map after removeIf
		System.out.println("\n\nAfter removing entry from Map : \n" + map);
	}
}

Output:

Before removing entry from Map : 
{1=Google, 2=YouTube, 3=Facebook, 4=Yahoo, 5=Amazon, 6=Reddit, 7=Twitter}


After removing entry from Map : 
{1=Google, 2=YouTube, 3=Facebook, 5=Amazon, 6=Reddit, 7=Twitter}

2. Removing all entries by comparing keys using Greater-Than (>) operator

RemovingAllEntriesGreaterThanByComparingKeys.java

package in.bench.resources.map.java8.example;

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

public class RemovingAllEntriesGreaterThanByComparingKeys {

	public static void main(String[] args) {

		// create HashMap
		Map<Integer, String> map = new HashMap<Integer, String>();

		// put few items
		map.put(1, "Google");
		map.put(2, "YouTube");
		map.put(3, "Facebook");
		map.put(4, "Yahoo");
		map.put(5, "Amazon");
		map.put(6, "Reddit");
		map.put(7, "Twitter");

		// printing map before removeIf
		System.out.println("Before removing entry from Map : \n" + map);

		// removing all entries whose rank greater-than 5
		map.entrySet().removeIf(
				ranks -> ranks.getKey().compareTo(Integer.valueOf(5)) > 0);

		// printing map after removeIf
		System.out.println("\n\nAfter removing entry from Map : \n" + map);
	}
}

Output:

Before removing entry from Map : 
{1=Google, 2=YouTube, 3=Facebook, 4=Yahoo, 5=Amazon, 6=Reddit, 7=Twitter}


After removing entry from Map : 
{1=Google, 2=YouTube, 3=Facebook, 4=Yahoo, 5=Amazon}

1.3 Removing all entries by comparing keys using Less-Than (<) operator

RemovingAllEntriesLessThanByComparingKeys.java

package in.bench.resources.map.java8.example;

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

public class RemovingAllEntriesLessThanByComparingKeys {

	public static void main(String[] args) {

		// create HashMap
		Map<Integer, String> map = new HashMap<Integer, String>();

		// put few items
		map.put(1, "Google");
		map.put(2, "YouTube");
		map.put(3, "Facebook");
		map.put(4, "Yahoo");
		map.put(5, "Amazon");
		map.put(6, "Reddit");
		map.put(7, "Twitter");

		// printing map before removeIf
		System.out.println("Before removing entry from Map : \n" + map);

		// removing all entries whose rank less-than 3
		map.entrySet().removeIf(
				ranks -> ranks.getKey().compareTo(Integer.valueOf(3)) < 0);

		// printing map after removeIf
		System.out.println("\n\nAfter removing entry from Map : \n" + map);
	}
}

Output:

Before removing entry from Map : 
{1=Google, 2=YouTube, 3=Facebook, 4=Yahoo, 5=Amazon, 6=Reddit, 7=Twitter}


After removing entry from Map : 
{3=Facebook, 4=Yahoo, 5=Amazon, 6=Reddit, 7=Twitter}

In the next article, we will discuss how to remove entry/entries from HashMap by comparing values using Java 8 APIs

Related Articles:

Hope, you found this article very helpful. If you have any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.

Happy Coding !!
Happy Learning !!

Java - Sorting Collection of String, StringBuffer and StringBuilder
Java 8 - How to remove an entry from HashMap by comparing values ?