Saturday, November 15, 2025
HomeLanguagesJavaConcurrentSkipListSet first() method in Java

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());
    }
}


Output:

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);
        }
    }
}


Output:

Exception :java.util.NoSuchElementException

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentSkipListSet.html#first–

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32401 POSTS0 COMMENTS
Milvus
95 POSTS0 COMMENTS
Nango Kala
6768 POSTS0 COMMENTS
Nicole Veronica
11919 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11990 POSTS0 COMMENTS
Shaida Kate Naidoo
6897 POSTS0 COMMENTS
Ted Musemwa
7149 POSTS0 COMMENTS
Thapelo Manthata
6850 POSTS0 COMMENTS
Umr Jansen
6841 POSTS0 COMMENTS