ConcurrentSkipListSet in Java with Examples - GeeksforGeeks

11 Nov, 2020
The ConcurrentSkipListSet class in Java is a part of the Java Collection Framework and implements the Collection interface and the AbstractSet class. It provides a scalable and concurrent version of NavigableSet in Java. The implementation of ConcurrentSkipListSet is based on ConcurrentSkipListMap. The elements in ConcurrentSkipListSet are sorted by default in their natural ordering or by a Comparator provided at set creation time, depending on which constructor is used.
Since it implements SortedSet<E> and NavigableSet<E>, it is similar to TreeSet with an added feature of being concurrent. Since it is a thread-safe, it can be used by multiple threads concurrently whereas TreeSet is not thread-safe.
Class Hierarchy: 
Declaration: 
public class ConcurrentSkipListSet<E>
    extends AbstractSet<E>
        implements NavigableSet<E>, Cloneable, Serializable

Where E is the type of elements maintained by this collection

Some important points on ConcurrentSkipListSet:
It implements Serializable, Cloneable, Iterable<E>, Collection<E>, NavigableSet<E>, Set<E>, SortedSet<E> interfaces.
It does not allow null elements, because null arguments and return values cannot be reliably distinguished from the absence of elements.
Its implementation provides average log(n) time cost for contains, add, and remove operations and their variants.
It is thread-safe.
It should be preferred over implementing Set interface when concurrent modification of set is required.
Constructors:
1. ConcurrentSkipListSet(): This constructor is used to construct an empty set.
ConcurrentSkipListSet<E> set = new ConcurrentSkipListSet<E>();
2. ConcurrentSkipListSet(Collection<E> c): This constructor is used to construct a set with the elements of the Collection passed as the parameter.
ConcurrentSkipListSet<E> set = new ConcurrentSkipListSet<E>(Collection<E> c);
3. ConcurrentSkipListSet(Comparator<E> comparator): This constructor is used to construct a new, empty set that orders its elements according to the specified comparator.
ConcurrentSkipListSet<E> set = new ConcurrentSkipListSet<E>(Comparator<E> comparator);
4. ConcurrentSkipListSet(SortedSet<E> s): This constructor is used to construct a new set containing the same elements and using the same ordering as the specified sorted set.
ConcurrentSkipListSet<E> set = new ConcurrentSkipListSet<E>(SortedSet<E> s);
Example 1:
Java
// Java program to demonstrate ConcurrentSkipListSet  import java.util.*;import java.util.concurrent.ConcurrentSkipListSet;  class ConcurrentSkipListSetLastExample1 {    public static void main(String[] args)    {          // Initializing the set using        // ConcurrentSkipListSet()        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 ConcurrentSkipListSet        System.out.println("ConcurrentSkipListSet: " + set);          // Initializing the set using        // ConcurrentSkipListSet(Collection)        ConcurrentSkipListSet<Integer> set1            = new ConcurrentSkipListSet<Integer>(set);          // Printing the ConcurrentSkipListSet1        System.out.println("ConcurrentSkipListSet1: "                           + set1);          // Initializing the set using        // ConcurrentSkipListSet()        ConcurrentSkipListSet<String> set2            = new ConcurrentSkipListSet<>();          // Adding elements to this set        set2.add("Apple");        set2.add("Lemon");        set2.add("Banana");        set2.add("Apple");          // creating an iterator        Iterator<String> itr = set2.iterator();          System.out.print("Fruits Set: ");        while (itr.hasNext()) {            System.out.print(itr.next() + " ");        }    }}
Output:
ConcurrentSkipListSet: [8, 12, 45, 64, 78]
ConcurrentSkipListSet1: [8, 12, 45, 64, 78]
Fruits Set: Apple Banana Lemon 
 Example 2:  
Java
// Java program to demonstrate ConcurrentSkipListSet  import java.util.concurrent.ConcurrentSkipListSet;  class ConcurrentSkipListSetLastExample1 {        public static void main(String[] args)    {          // Initializing the set using ConcurrentSkipListSet()        ConcurrentSkipListSet<Integer>            set = new ConcurrentSkipListSet<Integer>();          // Adding elements to this set        // using add() method        set.add(78);        set.add(64);        set.add(12);        set.add(45);        set.add(8);          // Printing the ConcurrentSkipListSet        System.out.println("ConcurrentSkipListSet: "                           + set);          // Printing the highest element of the set        // using last() method        System.out.println("The highest element of the set: "                           + set.last());          // Retrieving and removing first element of the set        System.out.println("The first element of the set: "                           + set.pollFirst());          // Checks if 9 is present in the set        // using contains() method        if (set.contains(9))            System.out.println("9 is present in the set.");        else            System.out.println("9 is not present in the set.");          // Printing the size of the set        // using size() method        System.out.println("Number of elements in the set = "                           + set.size());    }}
Output:
ConcurrentSkipListSet: [8, 12, 45, 64, 78]
The highest element of the set: 78
The first element of the set: 8
9 is not present in the set.
Number of elements in the set = 4
METHOD
DESCRIPTION
 subSet(E fromElement, boolean 
fromInclusive, E toElement, boolean toInclusive)
METHOD
DESCRIPTION
METHOD
DESCRIPTION
METHOD
DESCRIPTION
METHOD
DESCRIPTION
METHOD
DESCRIPTION
Ganeshchowdharysadanala
Java - util package
Java-Collections
Java-ConcurrentSkipListSet
Java-Functions
Java
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Object Oriented Programming (OOPs) Concept in Java
Arrays.sort() in Java with examples
Reverse a string in Java
HashMap in Java with Examples
Interfaces in Java
Stream In Java
How to iterate any Map in Java