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
-
How is ConcurrentSkipListSet concurrent?
ConcurrentSkipListSet is concurrent because the elements it holds cannot be written to at the same time by concurrently executing threads.
-
What is the hashCode() method?
It is a method inherited from class java.util.AbstractSet, which returns the hash code value for the set.
-
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.
-
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!