Given a map in Java, the task is to find out the entry in this map with the highest value.
Illustration:
Input : Map = {ABC = 10, DEF = 30, XYZ = 20} Output : DEF = 30
Input : Map = {1 = 40, 2 = 30, 3 = 60} Output : 3 = 60
Methods: There can be several approaches to achieve the goal that are listed as follows:
- Simple iterative approach via for each loop
- Using max() method from Collections class
- Using the concept of Streams introduced in Java 8
Now we will discuss the above-proposed methods alongside procedure and implementing them via clean java programs.
Method 1: Using iterative approach for each loop
Approach:
- Iterate the map entry by entry
- Store the first entry in a reference variable to compare to initially.
- If the current entry’s value is greater than the reference entry’s value, then store the current entry as the reference entry.
- Repeat this process for all the entries in the map.
- In the end, the reference variable has the required entry with the highest value in the map.
- Print this entry
for (Map.Entry entry : map.entrySet()) { // Operations }
Example:
Java
// Java program to Find Entry // with the Highest Value in Map // Using Comparators in Map interface // Importing all utility classes import java.util.*; // Main class class GFG { // Method 1 // Find the entry with highest value public static <K, V extends Comparable<V> > Map.Entry<K, V> getMaxEntryInMapBasedOnValue(Map<K, V> map) { // To store the result Map.Entry<K, V> entryWithMaxValue = null ; // Iterate in the map to find the required entry for (Map.Entry<K, V> currentEntry : map.entrySet()) { if ( // If this is the first entry, set result as // this entryWithMaxValue == null // If this entry's value is more than the // max value Set this entry as the max || currentEntry.getValue().compareTo( entryWithMaxValue.getValue()) > 0 ) { entryWithMaxValue = currentEntry; } } // Return the entry with highest value return entryWithMaxValue; } // Method 2 // To print the map public static void print(Map<String, Integer> map) { System.out.print( "Map: " ); // If map does not contain any value if (map.isEmpty()) { System.out.println( "[]" ); } else { System.out.println(map); } } // Method 3 // Main driver method public static void main(String[] args) { // Creating a Map // Declaring object of string and integer type Map<String, Integer> map = new HashMap<>(); // Inserting elements in the Map object // using put() method // Custom input element addition map.put( "ABC" , 10 ); map.put( "DEF" , 30 ); map.put( "XYZ" , 20 ); // Calling method 2 to // print the map print(map); // Calling method 1 to // find the entry with highest value and // print on the console System.out.println( "Entry with highest value: " + getMaxEntryInMapBasedOnValue(map)); } } |
Map: {ABC=10, DEF=30, XYZ=20} Entry with highest value: DEF=30
Method 2: Using max() method from Collections class without lambda expression
Example:
Java
// Java Program to Find Entry with largest Value in Map // Using max() method from Collections class // Importing required classes import java.util.*; // main class class GFG { // Main driver method public static void main(String[] args) { // Creating HashMap // Declaring objects of string and integer type HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); // Inserting elements in the Map // using put() method // Custom input addition map.put( 1 , 4 ); map.put( 2 , 5 ); map.put( 3 , 7 ); map.put( 4 , 2 ); // Using Collections.max() method returning max // value in HashMap and storing in a integer // variable int maxValueInMap = (Collections.max(map.values())); // Iterate through HashMap for (Entry<Integer, Integer> entry : map.entrySet()) { if (entry.getValue() == maxValueInMap) { // Print the key with max value System.out.println(entry.getKey()); } } } } |
Output:
3
Method 3: Using the concept of Streams introduced in Java 8
Java
// Java Program to Find Entry with largest Value in Map // Using concept of Streams import java.util.stream.*; // Main class class GFG { // Method 1 public static void main(String[] args) { // Entries in our map Map<String, String> map = new HashMap<>(); map.put( "A" , "23" ); map.put( "F" , "43" ); map.put( "C" , "56" ); map.put( "Z" , "04" ); // Printing the largest value in map by // calling above method System.out.print(map.maxUsingStreamAndLambda()); } // Method 2 public <String, String> extends Comparable<String> > maxUsingStreamAndLambda( Map<String, String> map) { // Using lambda operation over streams Map<String, String> maxEntry = map.entrySet().stream().max( (String e1, String e2) -> e1.getValue().compareTo( 0e2.getValue())); // Returning the maximum element from map return maxEntry.get().getValue(); } } |
Output:
C