ConcurrentSkipListSet in Java with Examples

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.

Class Hierarchy:

java.lang.Object
   ↳ java.util.AbstractCollection<E>
      ↳ java.util.AbstractSet<E>
         ↳ Class ConcurrentSkipListSet<E>

Syntax:



public class ConcurrentSkipListSet<E>
    extends AbstractSet<E>
        implements NavigableSet<E>, Cloneable, Serializable

Where E is the type of elements maintained 
by this collection

Constructors in Java ConcurrentSkipListSet:

  • ConcurrentSkipListSet(): This constructor is used to construct an empty set.
  • ConcurrentSkipListSet(Collection<E> c): This constructor is used to construct a set with the elements of the Collection passed as the parameter.
  • ConcurrentSkipListSet(Comparator<E> comparator): This constructor is used to construct a new, empty set that orders its elements according to the specified comparator.
  • 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.

Below is a sample program to illustrate ConcurrentSkipListSet in Java:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

ConcurrentSkipListSet: [8, 12, 45, 64, 78]
ConcurrentSkipListSet1: [8, 12, 45, 64, 78]

Methods in Java ConcurrentSkipListSet:

  1. add(E e): This method adds the specified element to this set if it is not already present.
  2. ceiling(E e): This method returns the least element in this set greater than or equal to the given element, or null if there is no such element.
  3. clear(): This method removes all of the elements from this set.
  4. clone(): This method returns a shallow copy of this ConcurrentSkipListSet instance.
  5. comparator(): This method returns the comparator used to order the elements in this set, or null if this set uses the natural ordering of its elements.
  6. contains(Object o): This method returns true if this set contains the specified element.
  7. descendingIterator(): This method returns an iterator over the elements in this set in descending order.
  8. descendingSet(): This method returns a reverse order view of the elements contained in this set.
  9. equals(Object o): This method compares the specified object with this set for equality.
  10. first(): This method returns the first (lowest) element currently in this set.
  11. floor(E e): This method returns the greatest element in this set less than or equal to the given element, or null if there is no such element.
  12. headSet(E toElement): This method returns a view of the portion of this set whose elements are strictly less than toElement.
  13. headSet(E toElement, boolean inclusive): This method returns a view of the portion of this set whose elements are less than (or equal to, if inclusive is true) toElement.
  14. higher(E e): This method returns the least element in this set strictly greater than the given element, or null if there is no such element.
  15. isEmpty(): This method returns true if this set contains no elements.
  16. iterator(): This method returns an iterator over the elements in this set in ascending order.
  17. last(): This method returns the last (highest) element currently in this set.
  18. lower(E e): This method returns the greatest element in this set strictly less than the given element, or null if there is no such element.
  19. pollFirst(): This method retrieves and removes the first (lowest) element, or returns null if this set is empty.
  20. pollLast(): This method retrieves and removes the last (highest) element, or returns null if this set is empty.
  21. remove(Object o): This method removes the specified element from this set if it is present.
  22. removeAll(Collection<E> c): This method removes from this set all of its elements that are contained in the specified collection.
  23. size(): This method returns the number of elements in this set.
  24. spliterator(): This method returns a Spliterator over the elements in this set.
  25. subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive): This method returns a view of the portion of this set whose elements range from fromElement to toElement.
  26. subSet(E fromElement, E toElement): This method returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive.
  27. tailSet(E fromElement): This method returns a view of the portion of this set whose elements are greater than or equal to fromElement.
  28. tailSet(E fromElement, boolean inclusive): This method returns a view of the portion of this set whose elements are greater than (or equal to, if inclusive is true) fromElement.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentSkipListSet.html



My Personal Notes arrow_drop_up

Technical Content Engineer at GeeksForGeeks

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.