Java 8 – How to remove an entry with Largest Key in a Map or HashMap ?

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

Remove an entry with Largest Key in a HashMap :

To remove an entry with Largest Key,

  • First sort the HashMap in descendingorder of Keys
  • Then get the 1st entry from the reversesorted Map
  • Finally, remove the 1st entry by comparing the Key

There are different ways to sort the Map/HashMap according to Keys, read below articles before proceeding further

Here, we will discuss only 2 approaches to remove an entry with Largest Key from HashMap

  • Using Java 8 Stream
  • Before Java 8

1. Using Java 8 Stream :

  • First step is to sort the Map entries in descendingorder of Keys which will give an entry with Largest Key at the top of the Map
    • To sort Map entries in descending-order of Keys, use Stream.sorted() method passing the argument Map.Entry.comparingByKey(Comparator.reverseOrder())
  • Get the 1st entry from sorted Map using Stream.findFirst() method
  • Finally, remove the 1st entry by comparing the Largest Key obtained from the previous step using removeIf() method

RemoveEntryWithLargestKeyInMapUsingJava8Stream.java

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

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class RemoveEntryWithLargestKeyInMapUsingJava8Stream {

	public static void main(String[] args) {

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


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


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


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



		// 2. Sort Keys using Java 8 Stream.sorted() method & get 1st Entry
		Entry<Integer, String> entryWithLargestKey = populationCountry
				.entrySet()
				.stream()
				.sorted(Map.Entry.comparingByKey(Comparator.reverseOrder()))
				.findFirst()
				.get();


		// 2.2 print Map.Entry with Largest Key
		System.out.println("\n\n2. Map Entry with Largest Key :- " + entryWithLargestKey);



		// 3. remove entry with largest key
		populationCountry
		.entrySet()
		.removeIf(entry -> entry.getKey().equals(entryWithLargestKey.getKey()));


		// 3.1 print to console
		System.out.println("\n\n3. Map entries after removing entry with Largest Key :- \n");


		// 3.2 print Map entries after removing entry with Largest Key
		populationCountry.forEach((key, value) -> System.out.println(
				"Key : " + key  + "\t\t"  + "Value : "  + value
				));
	}
}

Output :

1. Original Map Entries :- 

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


2. Map Entry with Largest Key :- 382357386=Indian


3. Map entries after removing entry with Largest Key :- 

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

2. Before Java 8 :

  • First step is to sort the Map entries in descendingorder of Keys which will give an entry with Largest Key at the top of the Map
    • To sort Map entries in descendingorder of Keys, convert HashMap to TreeMap passing Collections.reverseOrder() method as constructorargument to TreeMap object
  • Get the 1st entry from sorted Map by iterating using enhanced forloop and then break the loop
  • Finally, remove the 1st entry by comparing the Largest Key obtained from the previous step using remove() method

RemoveEntryWithLargestKeyInMap.java

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

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.Map.Entry;

public class RemoveEntryWithLargestKeyInMap {

	public static void main(String[] args) {

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


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


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


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



		// 2. convert HashMap to TreeMap in descending-order of Keys
		Map<Integer, String> treeMapInDescKeys = new TreeMap<>(Collections.reverseOrder());


		// 2.1  add unsorted map to TreeMap for reverse-order sorting of Keys
		treeMapInDescKeys.putAll(populationCountry);


		// 2.2 local variable to assign Entry with Largest Key
		Entry<Integer, String> entryWithLargestKey = null;


		// 2.3 iterate and get 1st entry from reverse-sorted Map
		for(Entry<Integer, String> entry : treeMapInDescKeys.entrySet()) {

			// first entry will be largest, as it sorted in descending-order of keys
			if(null == entryWithLargestKey) {
				entryWithLargestKey = entry;
				break;
			}
		}


		// 2.4 print Map.Entry with Largest Key
		System.out.println("\n\n2. Map Entry with Largest Key :- " + entryWithLargestKey);



		// 3. remove entry with largest key
		populationCountry
		.keySet()
		.remove(entryWithLargestKey.getKey());


		// 3.1 print to console
		System.out.println("\n\n3. Map entries after removing entry with Largest Key :- \n");


		// 3.1 print Map entries after removing entry with Largest Key
		populationCountry.forEach((key, value) -> System.out.println(
				"Key : " + key  + "\t\t"  + "Value : "  + value
				));
	}
}

Output :

1. Original Map Entries :- 

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


2. Map Entry with Largest Key :- 382357386=Indian


3. Map entries after removing entry with Largest Key :- 

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

Related Articles :

References :

Happy Coding !!
Happy Learning !!

Java 8 - How to find an entry with Largest Value in a Map or HashMap ?
Java 8 - How to find an entry with Largest Key in a Map or HashMap ?