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:
import java.util.concurrent.ConcurrentSkipListSet;
class ConcurrentSkipListSetLastExample1 {
public static void main(String[] args)
{
ConcurrentSkipListSet<Integer>
set = new ConcurrentSkipListSet<Integer>();
set.add( 78 );
set.add( 64 );
set.add( 12 );
set.add( 45 );
set.add( 8 );
System.out.println( "The highest element of the set: "
+ set.last());
}
}
|
Output:
The highest element of the set: 78
Program 2: Program to show NoSuchElementException in last().
import java.util.concurrent.ConcurrentSkipListSet;
class ConcurrentSkipListSetLastExample1 {
public static void main(String[] args)
{
ConcurrentSkipListSet<Integer>
set = new ConcurrentSkipListSet<Integer>();
try {
System.out.println( "The highest element of the set: "
+ set.last());
}
catch (Exception e) {
System.out.println( "Exception: " + e);
}
}
}
|
Output:
Exception: java.util.NoSuchElementException
Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentSkipListSet.html#last–