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
play_arrow
brightness_4
|
Output:
ConcurrentSkipListSet: [8, 12, 45, 64, 78] ConcurrentSkipListSet1: [8, 12, 45, 64, 78]
Methods in Java ConcurrentSkipListSet:
- add(E e): This method adds the specified element to this set if it is not already present.
- 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.
- clear(): This method removes all of the elements from this set.
- clone(): This method returns a shallow copy of this ConcurrentSkipListSet instance.
- 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.
- contains(Object o): This method returns true if this set contains the specified element.
- descendingIterator(): This method returns an iterator over the elements in this set in descending order.
- descending set(): This method returns a reverse order view of the elements contained in this set.
- equals(Object o): This method compares the specified object with this set for equality.
- first(): This method returns the first (lowest) element currently in this set.
- 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.
- headSet(E element): This method returns a view of the portion of this set whose elements are strictly less than toElement.
- headSet(E element, 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.
- 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.
- isEmpty(): This method returns true if this set contains no elements.
- iterator(): This method returns an iterator over the elements in this set in ascending order.
- last(): This method returns the last (highest) element currently in this set.
- 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.
- pollFirst(): This method retrieves and removes the first (lowest) element, or returns null if this set is empty.
- a poll last(): This method retrieves and removes the last (highest) element, or returns null if this set is empty.
- remove(Object o): This method removes the specified element from this set if it is present.
- removeAll(Collection<E> c): This method removes from this set all of its elements that are contained in the specified collection.
- size(): This method returns the number of elements in this set.
- spliterator(): This method returns a Spliterator over the elements in this set.
- the subSet(E fromElement, boolean from inclusive, E element, boolean inclusive): This method returns a view of the portion of this set whose elements range from element to element.
- the subSet(E fromElement, E element): This method returns a view of the portion of this set whose elements range from element, inclusive, to toElement, exclusive.
- toilet(E from the element): This method returns a view of the portion of this set whose elements are greater than or equal to from element.
- toilet(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) from an element.
Example:
filter_none
edit
play_arrow
brightness_4
|
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
post a comment