Java 8 – How to find an entry with Smallest Key in a Map or HashMap ?

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

Find an entry with Smallest Key in a HashMap :

To find an entry with Smallest Key,

  • First sort the HashMap in ascendingorder of Keys
  • Then get the 1st entry from the ascending-order sorted Map

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 find an entry with Smallest Key from HashMap

  • Using Java 8 Stream
  • Before Java 8

1. Using Java 8 Stream :

  • First step is to sort the Map entries in ascendingorder of Keys which will give an entry with Smallest Key at the top of the Map
    • To sort Map entries in ascending-order of Keys, use Stream.sorted() method passing the argument Map.Entry.comparingByKey(Comparator.naturalOrder())
  • Get the 1st entry from sorted Map using Stream.findFirst() method

FindEntryWithSmallestKeyInMapUsingJava8Stream.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 FindEntryWithSmallestKeyInMapUsingJava8Stream {

	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> entryWithSmallestKey = populationCountry
				.entrySet()
				.stream()
				.sorted(Map.Entry.comparingByKey(Comparator.naturalOrder()))
				.findFirst()
				.get();


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

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 Smallest Key :- 146748590=Russia

2. Before Java 8 :

  • First step is to sort the Map entries in ascendingorder of Keys which will give an entry with Smallest Key at the top of the Map
    • To sort Map entries in ascendingorder of Keys, convert HashMap to TreeMap which stores Map Entries in natural order of Keys
  • Get the 1st entry from sorted Map by iterating using enhanced forloop and then break the loop

FindEntryWithSmallestKeyInMap.java

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

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

public class FindEntryWithSmallestKeyInMap {

	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 ascending-order of Keys
		Map<Integer, String> treeMapInAscKeys = new TreeMap<>();


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


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


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

			// first entry will be smallest, as it sorted in ascending-order of keys
			if(null == entryWithSmallestKey) {
				entryWithSmallestKey = entry;
				break;
			}
		}


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

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 Smallest Key :- 146748590=Russia

Related Articles :

References :

Happy Coding !!
Happy Learning !!

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