VOOZH about

URL: https://www.geeksforgeeks.org/java/how-to-find-the-entry-with-largest-key-in-a-java-map/

⇱ How to find the Entry with largest Key in a Java Map - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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

Last Updated : 12 Jul, 2025

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

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

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
}
  1. Store the first entry in a reference variable to compare to initially.
  2. If the current entry's key is greater than the reference entry's value, then store the current entry as the reference entry.
  3. Repeat this process for all the entries in the map.
  4. In the end, the reference variable has the required entry with the highest key in the map.
  5. Print this entry

Below is the implementation of the above approach: 

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

Time complexity: O(NlogN) where N is size of map

Auxiliary Space: O(N)

Comment
Article Tags: