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 :
- Java – How to get maximum element from ArrayList ?
- Java – How to get minimum element from ArrayList ?
- Java – Find Largest number in an Arrays or List ?
- Java – Find Smallest number in an Arrays or List ?
- Java – Find 2nd Largest number in an Arrays or List ?
- Java – Find 2nd Smallest number in an Arrays or List ?
- Java – Find sum of Largest 2 numbers in an Arrays or List ?
- Java – Find sum of Smallest 2 numbers in an Arrays or List ?
- Java – Find 1st and Last elements in an Arrays ?
- Java – Find 1st and Last elements in a List or ArrayList ?
- Java – Find 1st and Last elements in a Set or HashSet ?
- Java – Find 1st and Last entries in a Map or HashMap ?
- Java – Find sum and average of a List or ArrayList ?
- Java – How to calculate sum and average of an Arrays ?
- Java – Find Maximum and Minimum Key/value in a Map ?
References :
- https://docs.oracle.com/javase/8/docs/api/java/util/Map.html
- https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html
- https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html#keySet–
- https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html#values–
- https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html
Happy Coding !!
Happy Learning !!