VOOZH about

URL: https://www.geeksforgeeks.org/java/treemap-lastkey-method-in-java/

⇱ TreeMap lastKey() Method in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

TreeMap lastKey() Method in Java

Last Updated : 9 Jul, 2018
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:
Output:
The Mappings are: {4=15, Geeks=20, Welcomes=25, You=30}
The last key is You
Comment
Article Tags: