The java.util.TreeMap.lastKey() is used to retrieve the last or the highest key present in the map.
Syntax: tree_map.lastKey()
Parameters: The method does not take any parameters.
Return Value: The method returns the last key present in the map.
Exception: The method throws
NoSuchElementException if the map is empty.
Below programs illustrate the working of java.util.TreeMap.lastKey() method:
Program 1:
Output:
The Mappings are: {10=Geeks, 15=4, 20=Geeks, 25=Welcomes, 30=You}
The last key is 30
Program 2: