Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Properties
3.
Class Hierarchy
3.1.
Syntax
4.
Constructors of ConcurrentSkipListSet
4.1.
Program
4.2.
Output
4.3.
Program
4.4.
Output
5.
Methods in ConcurrentSkipList
6.
FAQs
7.
Conclusion
Last Updated: Mar 27, 2024

ConcurrentSkipListSet

Author Anjali
0 upvote

Introduction

In this article, we will be discussing a crucial concept related to ConcurrentSkipListSet and its methods. This is a part of the Java Collection Framework and implements the Collection interface and the AbstractSet class. Its implementation is based on ConcurrentSkipListMap.It provides a scalable and concurrent version of NavigableSet in Java. The elements of ConcurrentSkipListSet are sorted by default in natural ordering or by the comparator provided at the time of set creation, depending on which constructor is used.

Since ConcurrentSkipListSet implements SortedSet<E> and NavigableSet<E>, hence it is similar to TreeSet with an additional feature of being concurrent. As it is thread-safe, it can be used by multiple threads concurrently whereas TreeSet is not thread-safe. 

Properties

  1. It implements Serializable, Cloneable, Collection<E>, NavigableSet<E>, Iterable<E>,  Set<E>, SortedSet<E> interfaces.
  2. It does not allow any null element, because null arguments and return values cannot be reliably distinguished from absence of elements.
  3. Its implementation provides average log(n) time cost for contains, add, and remove operations and their variants.
  4. It is thread-safe.
  5. It should be preferred over implementing Set interface when concurrent modification of set is required.

Class Hierarchy

java.lang.Object
   â†’ java.util.AbstractCollection<E>
       â†’ java.util.AbstractSet<E>
            â†’ java.util.concurrent.ConcurrentSkipListSet<E>

Syntax

public class ConcurrentSkipListSet<E>
    extends AbstractSet<E>
        implements NavigableSet<E>, Cloneable, Serializable
You can also try this code with Online Java Compiler
Run Code

Here, E is the type of elements stored in the Collection. 

Constructors of ConcurrentSkipListSet

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 containing 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 which orders the 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 using the same ordering as the specified sorted set.
ConcurrentSkipListSet<E> set = new ConcurrentSkipListSet<E>(SortedSet<E> s);

Program

// Java program to demonstrate ConcurrentSkipListSet
import java.util.*;
import java.util.concurrent.ConcurrentSkipListSet;

class ConcurrentSkipListSetProgram1 {
	public static void main(String[] args) {
		// Initializing the set using ConcurrentSkipListSet()
		ConcurrentSkipListSet<Integer> s = new ConcurrentSkipListSet<Integer>();

		// Adding elements to the set s
		s.add(708);
		s.add(624);
		s.add(124);
		s.add(453);
		s.add(825);

		// Printing the elements of ConcurrentSkipListSet
		System.out.println("ConcurrentSkipListSet: " + s);

		// Initializing the set using ConcurrentSkipListSet(Collection)
		ConcurrentSkipListSet<Integer> s1 = new ConcurrentSkipListSet<Integer>(s);

		// Printing the ConcurrentSkipListSet1
		System.out.println("ConcurrentSkipListSet1: " + s1);

		// Initializing the set using ConcurrentSkipListSet()
		ConcurrentSkipListSet<String> s2 = new ConcurrentSkipListSet<>();

		// Adding elements to this set
		s2.add("C++");
		s2.add("Python");
		s2.add("Java");
		s2.add("Kotlin");

		// creating an iterator
		Iterator<String> itr = s2.iterator();

		System.out.print("Programming Languages Set: ");
		while (itr.hasNext()) {
			System.out.print(itr.next() + " ");
		}
	}
}
You can also try this code with Online Java Compiler
Run Code

Output

Practice it on online java compiler for better understanding.

Program

// Java program to illustrate ConcurrentSkipListSet
import java.util.concurrent.ConcurrentSkipListSet;

class ConcurrentSkipListSetProgram2{
	public static void main(String[] args) {

		// Initializing the set using ConcurrentSkipListSet()
		ConcurrentSkipListSet<Integer> s = new ConcurrentSkipListSet<Integer>();

		// Adding elements to the set s using add() method
		s.add(708);
		s.add(642);
		s.add(1224);
		s.add(453);
		s.add(856);

		// Printing the ConcurrentSkipListSet
		System.out.println("ConcurrentSkipListSet: " + s);

		// Printing the highest element of the set s using last() method|
		System.out.println("The highest element of the set: " +  s.last());

        // Retrieving and removing first element of set using pollFirst() method
        System.out.println("The first element of the set: " + s.pollFirst());
		System.out.println("The first element of the set is removed. ");

		// Printing the ConcurrentSkipListSet
		System.out.println("ConcurrentSkipListSet: " + s);

		// Checks if 923 is present in the set s using contains() method
		if (s.contains(9))
			System.out.println("923 is present in the set.");
		else
			System.out.println("923 is not present in the set."); 
                   		
		// Printing the size of the set s using size() method
		System.out.println("Number of elements in the set = " + s.size());
	}
}
You can also try this code with Online Java Compiler
Run Code

Output

Methods in ConcurrentSkipList

FAQs

  1. How is ConcurrentSkipListSet concurrent?
    ConcurrentSkipListSet is concurrent because the elements it holds cannot be written to at the same time by concurrently executing threads.
     
  2. What is the hashCode() method?
    It is a method inherited from class java.util.AbstractSet, which returns the hash code value for the set.
     
  3. Which is faster among TreeSet and ConcurrentSkipListSet?
    TreeSet is fast as it is not synchronized while in comparison, ConcurrentSkipListSet is slow as it is synchronized.
     
  4. What is the iteration order in ConcurrentSkipListSet?
    ConcurrentSkipListSet does not preserve the insertion order of elements in the set, and the elements are kept sorted according to natural ordering of the elements or by provided Comparator. Hence iteration is done in the sorted order of elements.

Conclusion

In this blog, we have discussed ConcurrentSkipListSet. We discussed the syntax, constructors of the ConcurrentSkipListSet. Then we discussed the various other methods of the class ConcurrentSkipListSet with a few examples.

We hope that this blog helps you enhance your knowledge regarding ConcurrentSkipListSet. Learning never stops, and to learn more and become more skilled, head over to our practice platform Coding Ninjas Studio, to practice top problems attempt Mock Tests, read informative blogs, and interview experiences. Do upvote our blog to help other ninjas grow. 

Happy Learning!

Live masterclass