Java – How to find Maximum and Minimum Key/Value in a Map ?

In this article, we will discuss how to find maximum & minimum Map Key & Value

1. Maximum & Minimum Map Key :

  • Map.keySet() provides set of keys from a Map or HashMap
  • To find maximum & minimum Map Key, use Collections.max() and Collections.min() methods passing set of keys as argument

FindMaximumMinimumMapKey.java

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

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

public class FindMaximumMinimumMapKey {

	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. maximum Key in a Map
		int max = Collections.max(populationCountry.keySet());


		// 2.1 print to console
		System.out.println("\n\n2. Maximum Map Key is = " + max);



		// 3. maximum Key in a Map
		int min = Collections.min(populationCountry.keySet());


		// 3.1 print to console
		System.out.print("\n3. Minimum Map Key is = " + min);
	}
}

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. Maximum Map Key is = 382357386

3. Minimum Map Key is = 146748590

2. Maximum & Minimum Map Value :

  • Map.values() provides list of values from a Map or HashMap
  • To find maximum & minimum Map Value, use Collections.max() and Collections.min() methods passing list of values as argument

FindMaximumMinimumMapValue.java

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

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

public class FindMaximumMinimumMapValue {

	public static void main(String[] args) {

		// 1. creating HashMap object of type <Integer, String>
		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. Maximum Value in a Map
		int max = Collections.max(countryPopulation.values());


		// 2.1 print to console
		System.out.println("\n\n2. Maximum Map Value is = " + max);



		// 3. Maximum Value in a Map
		int min = Collections.min(countryPopulation.values());


		// 3.1 print to console
		System.out.print("\n3. Minimum Map Value is = " + min);
	}
}

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. Maximum Map Value is = 382357386

3. Minimum Map Value is = 146748590

Related Articles :

References :

Happy Coding !!
Happy Learning !!

Java – How to reverse a Queue ?
Java 8 - How to remove an entry based on the Value in a Map or HashMap ?