Related Articles
How to find the Entry with largest Key in a Java Map
• Last Updated : 25 Mar, 2020

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
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 key 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 key in the map.
6. Print this entry

Below is the implementation of the above approach:

 `// Java program to find entry``// with highest key in a map`` ` `import` `java.util.*;`` ` `public` `class` `GFG {`` ` `    ``// Find the entry with highest key``    ``public` `static` `, V> Map.Entry``    ``getMaxEntryInMapBasedOnKey(Map map)``    ``{``        ``// To store the result``        ``Map.Entry entryWithMaxKey = ``null``;`` ` `        ``// Iterate in the map to find the required entry``        ``for` `(Map.Entry currentEntry : map.entrySet()) {`` ` `            ``if` `(`` ` `                ``// If this is the first entry,``                ``// set the result as this``                ``entryWithMaxKey == ``null`` ` `                ``// If this entry's key is more than the max key``                ``// Set this entry as the max``                ``|| currentEntry.getKey()``                           ``.compareTo(entryWithMaxKey.getKey())``                       ``> ``0``) {`` ` `                ``entryWithMaxKey = currentEntry;``            ``}``        ``}`` ` `        ``// Return the entry with highest key``        ``return` `entryWithMaxKey;``    ``}`` ` `    ``// Print the map``    ``public` `static` `void` `print(Map 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 map``            ``= ``new` `HashMap<>();``        ``map.put(``"ABC"``, ``10``);``        ``map.put(``"DEF"``, ``30``);``        ``map.put(``"XYZ"``, ``20``);`` ` `        ``print(map);`` ` `        ``System.out.println(``            ``"Entry with highest key: "``            ``+ getMaxEntryInMapBasedOnKey(map));``    ``}``}`
Output:
```Map: {ABC=10, DEF=30, XYZ=20}
Entry with highest key: XYZ=20
```

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up