ConcurrentSkipListSet first() method in Java
The first() method of java.util.concurrent.ConcurrentSkipListSet is an in-built function in Java which returns the first (lowest) element currently in this set.
Syntax:
ConcurrentSkipListSet.first()
Return Value: The function returns the first (lowest) element currently in this set.
Exception: The function throws NoSuchElementException if this set is empty.
Below programs illustrate the ConcurrentSkipListSet.first() method:
Program 1:
// Java Program Demonstrate first() // method of ConcurrentSkipListSet import java.util.concurrent.ConcurrentSkipListSet; class ConcurrentSkipListSetFirstExample1 { public static void main(String[] args) { // Initializing the set ConcurrentSkipListSet<Integer> set = new ConcurrentSkipListSet<Integer>(); // Adding elements to first set set.add(10); set.add(35); set.add(20); set.add(25); System.out.println("The lowest element in the set: " + set.first()); } } |
The lowest element in the set: 10
Program 2: Program to show NoSuchElementException in first().
// Java Program Demonstrate first() // method of ConcurrentSkipListSet import java.util.concurrent.ConcurrentSkipListSet; class ConcurrentSkipListSetFirstExample2 { public static void main(String[] args) throws InterruptedException { // Initializing the set ConcurrentSkipListSet<Integer> set = new ConcurrentSkipListSet<Integer>(); try { System.out.println("The lowest element in the set: " + set.first()); } 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#first–
Recommended Posts:
- ConcurrentSkipListSet add() method in Java
- ConcurrentSkipListSet last() method in Java
- ConcurrentSkipListSet contains() method in Java
- ConcurrentSkipListSet spliterator() method in Java
- ConcurrentSkipListSet headSet() method in Java
- ConcurrentSkipListSet descendingIterator() method in Java
- ConcurrentSkipListSet subSet() method in Java
- ConcurrentSkipListSet removeAll() method in Java
- ConcurrentSkipListSet pollLast() method in Java
- ConcurrentSkipListSet pollFirst() method in Java
- ConcurrentSkipListSet higher() method in Java
- ConcurrentSkipListSet floor() method in Java
- ConcurrentSkipListSet iterator() method in Java
- ConcurrentSkipListSet equals() method in Java
- ConcurrentSkipListSet size() 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.



