How to find the Entry with largest Value in a Java Map

Given a map in Java, the task is to find out the entry in this map with the highest value.

Examples:

Input: Map = {ABC = 10, DEF = 30, XYZ = 20}
Output: DEF = 30

Input: Map = {1 = 40, 2 = 30, 3 = 60}
Output: 3 = 60

Approach

  1. Iterate the map entry by entry
    for (Map.Entry entry : map.entrySet()) 
    {
        // Operations
    }
    
  2. Store the first entry in a reference variable to compare to initially.
  3. If the current entry’s value is greater than the reference entry’s value, then store the current entry as the reference entry.
  4. Repeat this process for all the entries in the map.
  5. In the end, the reference variable has the required entry with the highest value in the map.
  6. Print this entry

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find entry
// with the highest value in a map
  
import java.util.*;
  
public class GFG {
  
    // 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;
    }
  
    // 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);
        }
    }
  
    // Driver code
    public static void main(String[] args)
    {
  
        Map<String, Integer> map
            = new HashMap<>();
        map.put("ABC", 10);
        map.put("DEF", 30);
        map.put("XYZ", 20);
  
        print(map);
  
        System.out.println(
            "Entry with highest value: "
            + getMaxEntryInMapBasedOnValue(map));
    }
}

chevron_right


Output:

Map: {ABC=10, DEF=30, XYZ=20}
Entry with highest value: DEF=30

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.