VOOZH about

URL: https://www.geeksforgeeks.org/java/sortedset-last-method-in-java/

⇱ SortedSet last() method in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

SortedSet last() method in Java

Last Updated : 11 Jul, 2025

The last() method of SortedSet interface in Java is used to return the last i.e., the highest element currently in this set.
Syntax
 

E last()


Where, E is the type of element maintained by this Set.
Parameters: This function does not accepts any parameter.
Return Value: It returns the last or the highest element currently in the set.
Exceptions: It throws NoSuchElementException, if the set is empty.
Below programs illustrate the above method:
Program 1
 


Output: 
Greatest element in set is : 9

 

Program 2
 


Output: 
Exception: java.util.NoSuchElementException

 

Reference: https://docs.oracle.com/javase/10/docs/api/java/util/SortedSet.html#last()
 

Comment