ConcurrentSkipListSet last() method in Java
The last() method of java.util.concurrent.ConcurrentSkipListSet is an in-built function in Java which returns the last (highest) element currently in this set.
Syntax:
public E last()
Return Value: The function returns returns the last (highest) element currently in this set.
Exception: The function throws the NoSuchElementException if this set is empty.
Below programs illustrate the ConcurrentSkipListSet.last() method:
Program 1:
// Java program to demonstrate last() // method of ConcurrentSkipListSet import java.util.concurrent.ConcurrentSkipListSet; class ConcurrentSkipListSetLastExample1 { public static void main(String[] args) { // Initializing the set ConcurrentSkipListSet<Integer> set = new ConcurrentSkipListSet<Integer>(); // Adding elements to this set set.add(78); set.add(64); set.add(12); set.add(45); set.add(8); // Printing the highest element of the set System.out.println("The highest element of the set: " + set.last()); } } |
The highest element of the set: 78
Program 2: Program to show NoSuchElementException in last().
// Java program to demonstrate last() // method of ConcurrentSkipListSet import java.util.concurrent.ConcurrentSkipListSet; class ConcurrentSkipListSetLastExample1 { public static void main(String[] args) { // Initializing the set ConcurrentSkipListSet<Integer> set = new ConcurrentSkipListSet<Integer>(); try { // Printing the highest element of the set System.out.println("The highest element of the set: " + set.last()); } catch (Exception e) { System.out.println("Exception: " + e); } } } |
Exception: java.util.NoSuchElementException
Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentSkipListSet.html#last–
Recommended Posts:
- ConcurrentSkipListSet contains() method in Java
- ConcurrentSkipListSet first() method in Java
- ConcurrentSkipListSet add() method in Java
- ConcurrentSkipListSet clone() method in Java
- ConcurrentSkipListSet isEmpty() method in Java
- ConcurrentSkipListSet clear() method in Java
- ConcurrentSkipListSet size() method in Java
- ConcurrentSkipListSet spliterator() method in Java
- ConcurrentSkipListSet equals() method in Java
- ConcurrentSkipListSet remove() method in Java
- ConcurrentSkipListSet descendingSet() method in Java
- ConcurrentSkipListSet ceiling() method in Java
- ConcurrentSkipListSet descendingIterator() method in Java
- ConcurrentSkipListSet floor() method in Java
- ConcurrentSkipListSet headSet() method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



