Javatpoint Logo
Javatpoint Logo

ConcurrentSkipListSet in Java

The Java ConcurrentSkipListSet class implements the Collection interface and the AbstractSet class as a component of the Java Collection Framework. It offers the concurrent, scalable version of NavigableSet in Java. ConcurrentSkipListSet is based on ConcurrentSkipListMap in its implementation. Depending on whether constructor is used, the entries in the ConcurrentSkipListSet are either sorted by default in thier natural ordering or by a Comparator specified at set construction time.

It is comparable to TreeSet in that it implements SortedSet and NavigableSet, but it also has the feature of concurrent. As contrast to TreeSet, which is not thread-safe, it may be used by many threads concurrently.

Declaration:

Important points on ConcurrentSkipListSet:

  • ConcurrentSkipListSet is a thread-safe implementation of the Set interface in Java. It implies that the set can be concurrently accessed and modified by multiple threads without requiring explicit synchronization.
  • ConcurrentSkipListSet stores elements in a sorted order, which can be customized using a Comparator object. Maintaining a sorted collection of data is a common use case for this method.
  • ConcurrentSkipListSet allows for null elements, but because null elements are not comparable, it's important to use a non-null Comparator when initializing the set.
  • ConcurrentSkipListSet provides methods to perform operations like adding and removing elements, checking if an element is in the set, and retrieving the first or last element in the set.
  • ConcurrentSkipListSet is part of the java.util.concurrent package, which provides a collection of thread-safe data structures and utilities for concurrent programming in Java.

Constructors:

ConcurrentSkipListSet provides several constructors that can be used to create new instances of the class. Here are the constructors available in Java 8:

ConcurrentSkipListSet(): Creates an empty ConcurrentSkipListSet that orders its elements according to their natural ordering.

Filename: ConcurrentSkipListSetExample.java

Output:

Elements in the set:applebanana
orange
Updated elements in the set:
apple
orange
Does the set contain 'apple'? true
Does the set contain 'banana'? false
First element in the set: apple
Last element in the set: orange

ConcurrentSkipListSet(Comparator<? super E> comparator): Creates an empty ConcurrentSkipListSet that orders its elements according to the specified Comparator object.

Filename: ConcurrentSkipListSetComparatorExample.java

Output:

Elements in the set (in descending order):
orange
banana
apple

ConcurrentSkipListSet(Collection<? extends E> c): Creates a new ConcurrentSkipListSet that contains the elements of the specified collection, ordered according to their natural ordering.

Filename: ConcurrentSkipListSetCollectionExample.java

Output:

Elements in the set (in ascending order):
apple
banana
orange
peach
pear
Elements in the descending set (in descending order):
apple
banana
orange
peach
pear
Elements in the difference set (in ascending order):
grape
kiwi

ConcurrentSkipListSet(SortedSet<E> s): Creates a new ConcurrentSkipListSet that contains the same elements and orders them according to the same ordering as the specified SortedSet object.

Filename: ConcurrentSkipListSetSortedSetExample.java

Output:

Elements in the set:1
3
4
5
Updated elements in the set:
1
4
5
Does the set contain 1? true
Does the set contain 3? falseFirst element in the set: 1Last element in the set: 5
Updated elements in the set:1
2
4
5
6
Elements in the sub-set:
2
4

Example 1:

Filename: ConcurrentSkipListSetDemo.java

Output:

Initial set: [bird, cat, dog, fish]
Set after adding 'zebra': [bird, cat, dog, fish, zebra]
Set after removing 'cat': [bird, dog, fish, zebra]

Example 2:

Filename: ConcurrentSkipListSetExample2.java

Output:

Initial set: [1, 3, 5, 7]
First element: 1
Last element: 7
Set after adding other set: [1, 2, 3, 4, 5, 6, 7]
Set after removing first and last: [2, 3, 4, 5, 6]

Methods of ConcurrentSkipListSet

Method Description
add(E e) Inserts the specified element into this set if it is not already present.
addAll(Collection<? extends E> c) Adds all of the elements in the specified collection to this set if they're not already present.
ceiling(E e) The ceiling(E e) method of ConcurrentSkipListSet returns the least element in this set greater than or equal to the given element e, or null if there is no such element.
clear() The clear() method of ConcurrentSkipListSet removes all the elements from the set. The set will be empty after this method call. The operation is performed atomically.
comparator() The comparator() method of ConcurrentSkipListSet returns the comparator used to order the elements in the set, or null if the natural ordering of the elements is used.
contains(Object o) The contains(Object o) method of ConcurrentSkipListSet returns true if this set contains the specified element, and false otherwise. More formally, it returns true if and only if this set contains an element e such that (o==null ? e==null : o.equals(e)).
descendingIterator() The descendingIterator() method of ConcurrentSkipListSet returns an iterator over the elements in the set in reverse order.
descendingSet() The descendingSet() method of ConcurrentSkipListSet returns a reverse order view of the elements in the set.
first() Returns the first (lowest) element currently in this set.
floor(E e) Returns the greatest element in this set less than or equal to the given element, or null if there is no such element.
headSet(E toElement) Returns a view of the portion of this set whose elements are strictly less than toElement.
headSet(E toElement, boolean inclusive) The headSet(E toElement, boolean inclusive) method of ConcurrentSkipListSet returns a view of the portion of this set whose elements are strictly less than toElement. If the inclusive parameter is true, then the returned set includes the element toElement as well. A new view of the original set is provided by this method, without modifying the original set.
higher(E e) Returns the least element in this set strictly greater than the given element, or null if there is no such element.
isEmpty() Returns true if this set contains no elements.
iterator() Returns an iterator over the elements in this set in ascending order.
last() Returns the last (highest) element currently in this set.
lower(E e) Returns the greatest element in this set strictly less than the given element, or null if there is no such element.
pollFirst() Retrieves and removes the first (lowest) element, or returns null if this set is empty.
pollLast() Retrieves and removes the last (highest) element, or returns null if this set is empty.
remove(Object o) Removes the specified element from this set if it is present.
size() Returns the number of elements in this set.
subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) Returns a view of the portion of this set whose elements range from fromElement to toElement.
subSet(E fromElement, E toElement) Returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive.
tailSet(E fromElement) Returns a view of the portion of this set whose elements are greater than or equal to fromElement.
tailSet(E fromElement, boolean inclusive) Returns a view of the portion of this set whose elements are greater than (or equal to, if inclusive is true) fromElement.

Methods inherited from class java.util.AbstractSet

Method Description
hashCode() Returns the hash code value for this set. The hash code of a set is defined to be the sum of the hash codes of the elements in the set. Null elements are not counted towards the hash code. For interoperability with the Java collections framework, the method is provided.

Methods inherited from class java.util.AbstractCollection

Method Description
addAll(Collection<? extends E> c) The addAll(Collection<? extends E> c) method of the AbstractCollection class adds all elements in the specified collection to this collection, if they are not already present. Execution of this operation is not mandatory and its availability cannot be guaranteed across all implementations of the AbstractCollection interface.
containsAll(Collection<?> c) The containsAll(Collection<?> c) method of the AbstractCollection class returns a boolean value indicating whether or not this collection contains all of the elements in the specified collection.
retainAll(Collection<?> c) The retainAll(Collection<?> c) method of AbstractCollection removes all elements from the collection except those that are contained in the specified collection. In other words, it retains only the elements that are common to both collections. An implementation of AbstractCollection may not guarantee the execution of this method, as it is an optional operation. The method returns true if the collection was modified as a result of the operation, otherwise false.
toArray() The toArray() method of the Collection interface returns an array containing all the elements of the collection in the order they are returned by the collection's iterator. The returned array will have a length equal to the size of the collection. If the collection is empty, the method returns an empty array.
toArray(T[] a) The toArray(T[] a) method of the Collection interface returns an array containing all of the elements in this collection. If the specified array is large enough to hold all of the elements, the specified array is used, otherwise, a new array of the same runtime type is allocated for this purpose.
toString() The toString() method of AbstractCollection returns a string representation of the collection. The string representation consists of a list of the collection's elements enclosed in square brackets ([]). Adjacent elements are separated by the characters ", " (comma and space).

Methods inherited from interface java.util.Set

Method Description
addAll(Collection<? extends E> c) The addAll(Collection<? extends E> c) method of ConcurrentSkipListSet adds all the elements from the specified collection to the set, but only if they do not already exist in the set. It is not guaranteed to be executed by all implementations of ConcurrentSkipListSet and is considered an optional operation.
containsAll(Collection<?> c) The containsAll(Collection<?> c) method of ConcurrentSkipListSet checks if all the elements in the specified collection are present in the set. If all the elements are present, it returns true; otherwise, it returns false. The elements are compared using their natural ordering or the ordering provided by the comparator that was passed while creating the ConcurrentSkipListSet instance.
hashCode() The hashCode() method of ConcurrentSkipListSet returns the hash code value for the set. The hash code of a set is defined as the sum of the hash codes of its elements. Two sets that are equal according to the equals() method should return the same hash code value. The hash code value of the empty set is defined to be zero.
retainAll(Collection<?> c) The retainAll(Collection<?> c) method of ConcurrentSkipListSet retains only the elements in the set that are also contained in the specified collection. In other words, it removes all elements from the set that are not present in the specified collection. It is an optional operation.
toArray() The toArray() method of ConcurrentSkipListSet returns an array containing all the elements of the set in the same order as they appear in the set. The returned array is an Object array and its runtime type is not specified.
toArray(T[] a) The toArray(T[] a) method of ConcurrentSkipListSet returns an array containing all the elements in the set. The runtime type of the returned array is that of the specified array. If the set fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this set.

Methods inherited from interface java.util.Collection

Method Description
parallelStream() The parallelStream() method returns a possibly parallel stream with this collection as its source. The parallelStream() method is a part of the Collection interface and enables parallel processing of elements within the collection.
removeIf(Predicate<? super E> filter) The removeIf(Predicate<? super E> filter) method of the Collection interface removes all elements of the collection that satisfy the given predicate. It returns true if any elements were removed, else it returns false. The method throws a NullPointerException if the specified predicate is null.
stream() The stream() method of the Collection interface returns a sequential stream with the elements of this collection as its source. The order of the stream elements is determined by the collection's iteration order.

Methods inherited from interface java.lang.Iterable

Method Description
forEach The forEach method is inherited from the Iterable interface and applies the given action to each element of the collection in the order they are encountered. The action is performed for each element until all elements have been processed or the action throws an exception.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA