Saturday, October 25, 2025
HomeLanguagesJavaConcurrentSkipListSet add() method in Java

ConcurrentSkipListSet add() method in Java

The java.util.concurrent.ConcurrentSkipListSet.add() method is an in-built function in Java which is used to insert an element in this set.

Syntax:

ConcurrentSkipListSet.add(E e)

Parameters: The function accepts a single parameter e i.e. the element to be inserted.

Return Value: The function returns a True boolean value.

Below programs illustrate the ConcurrentSkipListSet.add() method:

Program 1: Adding Integer in the set.




// Java program to demonstrate add()
// method of ConcurrentSkipListSet
  
import java.util.concurrent.*;
  
class ConcurrentSkipListSetAddExample1 {
    public static void main(String[] args)
    {
        // Creating a set object
        ConcurrentSkipListSet<Integer> Lset = 
                  new ConcurrentSkipListSet<Integer>();
  
        // Adding elements to this set
        for (int i = 10; i <= 50; i += 10)
            Lset.add(i);
  
        // Printing elements of the set
        System.out.println("The set contains: ");
        for (Integer i : Lset)
            System.out.print(i + " ");
    }
}


Output:

The set contains: 
10 20 30 40 50

Program 2: Adding String in the set.




// Java program to demonstrate add()
// method of ConcurrentSkipListSet
  
import java.util.concurrent.*;
  
class ConcurrentSkipListSetAddExample2 {
    public static void main(String[] args)
    {
        // Creating a set object
        ConcurrentSkipListSet<String> Lset = 
                     new ConcurrentSkipListSet<String>();
  
        // Adding elements to this set
        Lset.add("alex");
        Lset.add("bob");
        Lset.add("chuck");
        Lset.add("drake");
        Lset.add("eric");
  
        // Printing elements of the set
        System.out.println("The set contains: ");
        for (String i : Lset)
            System.out.print(i + " ");
    }
}


Output:

The set contains: 
alex bob chuck drake eric

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentSkipListSet.html#add(E)

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS