E
- the type of elements maintained by this setpublic class ConcurrentSkipListSet<E> extends AbstractSet<E> implements NavigableSet<E>, Cloneable, Serializable
NavigableSet
implementation based on
a ConcurrentSkipListMap
. This class maintains a set in
ascending order, sorted according to the natural order for
the element's class (see Comparable
), or by the comparator
provided at creation time, depending on which constructor is
used.
This implementation provides expected average log(n) time
cost for the contains, add, and remove
operations and their variants. Insertion, removal, and access
operations safely execute concurrently by multiple
threads. Iterators are weakly consistent, returning elements
reflecting the state of the set at some point at or since the
creation of the iterator. They do not throw ConcurrentModificationException
, and may proceed concurrently with
other operations.
Beware that, unlike in most collections, the size method is not a constant-time operation. Because of the asynchronous nature of these sets, determining the current number of elements requires a traversal of the elements. Additionally, the bulk operations addAll, removeAll, <retainAll, and tt>containsAll are not guaranteed to be performed atomically. For example, an iterator operating concurrently with an addAll operation might view only some of the added elements.
This class and its iterators implement all of the
optional methods of the Set
and Iterator
interfaces. Like most other concurrent collection implementations,
this class does not permit the use of null elements.
because null arguments and return values cannot be reliably
distinguished from the absence of elements.
Constructor and Description |
---|
ConcurrentSkipListSet()
Constructs a new, empty set, sorted according to the elements' natural
order.
|
ConcurrentSkipListSet(Collection<? extends E> c)
Constructs a new set containing the elements in the specified
collection, sorted according to the elements' natural order.
|
ConcurrentSkipListSet(Comparator<? super E> c)
Constructs a new, empty set, sorted according to the specified
comparator.
|
ConcurrentSkipListSet(SortedSet<E> s)
Constructs a new set containing the same elements as the specified
sorted set, sorted according to the same ordering.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(E o)
Adds the specified element to this set if it is not already present.
|
E |
ceiling(E o)
Returns an element greater than or equal to the given element, or
null if there is no such element.
|
void |
clear()
Removes all of the elements from this set.
|
Object |
clone()
Returns a shallow copy of this set.
|
Comparator<? super E> |
comparator()
Returns the comparator used to order this set, or null
if this set uses its elements natural ordering.
|
boolean |
contains(Object o)
Returns true if this set contains the specified element.
|
Iterator<E> |
descendingIterator()
Returns an iterator over the elements in this set.
|
boolean |
equals(Object o)
Compares the specified object with this set for equality.
|
E |
first()
Returns the first (lowest) element currently in this set.
|
E |
floor(E o)
Returns an element less than or equal to the given element, or
null if there is no such element.
|
NavigableSet<E> |
headSet(E toElement)
Returns a view of the portion of this set whose elements are strictly
less than toElement.
|
E |
higher(E o)
Returns an element strictly greater than the given element, or
null if there is no such element.
|
boolean |
isEmpty()
Returns true if this set contains no elements.
|
Iterator<E> |
iterator()
Returns an iterator over the elements in this set.
|
E |
last()
Returns the last (highest) element currently in this set.
|
E |
lower(E o)
Returns an element strictly less than the given element, or
null if there is no such element.
|
E |
pollFirst()
Retrieves and removes the first (lowest) element.
|
E |
pollLast()
Retrieves and removes the last (highest) element.
|
boolean |
remove(Object o)
Removes the specified element from this set if it is present.
|
boolean |
removeAll(Collection<?> c)
Removes from this set all of its elements that are contained in
the specified collection.
|
int |
size()
Returns the number of elements in this set.
|
NavigableSet<E> |
subSet(E fromElement,
E toElement)
Returns a view of the portion of this set whose elements range from
fromElement, inclusive, to toElement, exclusive.
|
NavigableSet<E> |
tailSet(E fromElement)
Returns a view of the portion of this set whose elements are
greater than or equal to fromElement.
|
hashCode
addAll, containsAll, retainAll, toArray, toArray, toString
public ConcurrentSkipListSet()
public ConcurrentSkipListSet(Comparator<? super E> c)
c
- the comparator that will be used to sort this set. A
null value indicates that the elements' natural
ordering should be used.public ConcurrentSkipListSet(Collection<? extends E> c)
c
- The elements that will comprise the new set.ClassCastException
- if the elements in the specified
collection are not comparable, or are not mutually comparable.NullPointerException
- if the specified collection is
null.public ConcurrentSkipListSet(SortedSet<E> s)
s
- sorted set whose elements will comprise the new set.NullPointerException
- if the specified sorted set is
null.public Object clone()
public int size()
Beware that, unlike in most collections, this method is NOT a constant-time operation. Because of the asynchronous nature of these sets, determining the current number of elements requires traversing them all to count them. Additionally, it is possible for the size to change during execution of this method, in which case the returned result will be inaccurate. Thus, this method is typically not very useful in concurrent applications.
size
in interface Collection<E>
size
in interface Set<E>
size
in class AbstractCollection<E>
public boolean isEmpty()
isEmpty
in interface Collection<E>
isEmpty
in interface Set<E>
isEmpty
in class AbstractCollection<E>
public boolean contains(Object o)
contains
in interface Collection<E>
contains
in interface Set<E>
contains
in class AbstractCollection<E>
o
- the object to be checked for containment in this set.ClassCastException
- if the specified object cannot be compared
with the elements currently in the set.NullPointerException
- if o is null.public boolean add(E o)
add
in interface Collection<E>
add
in interface Set<E>
add
in class AbstractCollection<E>
o
- element to be added to this set.ClassCastException
- if the specified object cannot be compared
with the elements currently in the set.NullPointerException
- if o is null.public boolean remove(Object o)
remove
in interface Collection<E>
remove
in interface Set<E>
remove
in class AbstractCollection<E>
o
- object to be removed from this set, if present.ClassCastException
- if the specified object cannot be compared
with the elements currently in the set.NullPointerException
- if o is null.public void clear()
clear
in interface Collection<E>
clear
in interface Set<E>
clear
in class AbstractCollection<E>
public Iterator<E> iterator()
public Iterator<E> descendingIterator()
descendingIterator
in interface NavigableSet<E>
public boolean equals(Object o)
equals
in interface Collection<E>
equals
in interface Set<E>
equals
in class AbstractSet<E>
o
- Object to be compared for equality with this set.public boolean removeAll(Collection<?> c)
removeAll
in interface Collection<E>
removeAll
in interface Set<E>
removeAll
in class AbstractSet<E>
c
- collection that defines which elements will be removed from
this set.ClassCastException
- if the types of one or more elements in this
set are incompatible with the specified collectionNullPointerException
- if the specified collection, or any
of its elements are null.public E ceiling(E o)
ceiling
in interface NavigableSet<E>
o
- the value to matchClassCastException
- if o cannot be compared with the elements
currently in the set.NullPointerException
- if o is nullpublic E lower(E o)
lower
in interface NavigableSet<E>
o
- the value to matchClassCastException
- if o cannot be compared with the elements
currently in the set.NullPointerException
- if o is null.public E floor(E o)
floor
in interface NavigableSet<E>
o
- the value to matchClassCastException
- if o cannot be compared with the elements
currently in the set.NullPointerException
- if o is null.public E higher(E o)
higher
in interface NavigableSet<E>
o
- the value to matchClassCastException
- if o cannot be compared with the elements
currently in the set.NullPointerException
- if o is null.public E pollFirst()
pollFirst
in interface NavigableSet<E>
public E pollLast()
pollLast
in interface NavigableSet<E>
public Comparator<? super E> comparator()
comparator
in interface SortedSet<E>
public E first()
first
in interface SortedSet<E>
NoSuchElementException
- sorted set is empty.public E last()
last
in interface SortedSet<E>
NoSuchElementException
- sorted set is empty.public NavigableSet<E> subSet(E fromElement, E toElement)
subSet
in interface SortedSet<E>
subSet
in interface NavigableSet<E>
fromElement
- low endpoint (inclusive) of the subSet.toElement
- high endpoint (exclusive) of the subSet.ClassCastException
- if fromElement and
toElement cannot be compared to one another using
this set's comparator (or, if the set has no comparator,
using natural ordering).IllegalArgumentException
- if fromElement is
greater than toElement.NullPointerException
- if fromElement or
toElement is null.public NavigableSet<E> headSet(E toElement)
headSet
in interface SortedSet<E>
headSet
in interface NavigableSet<E>
toElement
- high endpoint (exclusive) of the headSet.ClassCastException
- if toElement is not compatible
with this set's comparator (or, if the set has no comparator,
if toElement does not implement Comparable).NullPointerException
- if toElement is null.public NavigableSet<E> tailSet(E fromElement)
tailSet
in interface SortedSet<E>
tailSet
in interface NavigableSet<E>
fromElement
- low endpoint (inclusive) of the tailSet.ClassCastException
- if fromElement is not
compatible with this set's comparator (or, if the set has no
comparator, if fromElement does not implement
Comparable).NullPointerException
- if fromElement is null.