E - the type of elements held in this collectionpublic class ConcurrentDoublyLinkedList<E> extends AbstractCollection<E> implements Serializable
Deque (double-ended
queue). Concurrent insertion, removal, and access operations execute safely
across multiple threads. Iterators are weakly consistent, returning
elements reflecting the state of the deque at some point at or since the
creation of the iterator. They do not throw ConcurrentModificationException, and may proceed concurrently with other
operations.
This class and its iterators implement all of the optional methods
of the Collection and Iterator interfaces. Like most other
concurrent collection implementations, this class does not permit the use of
null elements. because some null arguments and return values
cannot be reliably distinguished from the absence of elements. Arbitrarily,
the Collection.remove(java.lang.Object) method is mapped to
removeFirstOccurrence, and Collection.add(E) is mapped to
addLast.
Beware that, unlike in most collections, the size method is NOT a constant-time operation. Because of the asynchronous nature of these deques, determining the current number of elements requires a traversal of the elements.
This class is Serializable, but relies on default serialization mechanisms. Usually, it is a better idea for any serializable class using a ConcurrentLinkedDeque to instead serialize a snapshot of the elements obtained by method toArray.
| Modifier and Type | Class and Description |
|---|---|
static interface |
ConcurrentDoublyLinkedList.Node |
| Constructor and Description |
|---|
ConcurrentDoublyLinkedList()
Constructs an empty deque.
|
ConcurrentDoublyLinkedList(Collection<? extends E> c)
Constructs a deque containing the elements of the specified collection,
in the order they are returned by the collection's iterator.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
add(E e) |
boolean |
addAll(Collection<? extends E> c)
Appends all of the elements in the specified collection to the end of
this deque, in the order that they are returned by the specified
collection's iterator.
|
ConcurrentDoublyLinkedList.Node |
addFirst(E o)
Prepends the given element at the beginning of this deque.
|
ConcurrentDoublyLinkedList.Node |
addLast(E o)
Appends the given element to the end of this deque.
|
void |
clear()
Removes all of the elements from this deque.
|
boolean |
contains(Object o)
Returns true if this deque contains at least one element
e such that o.equals(e).
|
E |
element() |
E |
getFirst()
Returns the first element in this deque.
|
E |
getLast()
Returns the last element in this deque.
|
boolean |
isEmpty()
Returns true if this collection contains no elements.
|
Iterator<E> |
iterator()
Returns a weakly consistent iterator over the elements in this deque, in
first-to-last order.
|
boolean |
offer(E e) |
boolean |
offerFirst(E o)
Prepends the given element at the beginning of this deque.
|
boolean |
offerLast(E o)
Appends the given element to the end of this deque.
|
E |
peek() |
E |
peekFirst()
Retrieves, but does not remove, the first element of this deque, or
returns null if this deque is empty.
|
E |
peekLast()
Retrieves, but does not remove, the last element of this deque, or
returns null if this deque is empty.
|
E |
poll() |
E |
pollFirst()
Retrieves and removes the first element of this deque, or returns null if
this deque is empty.
|
E |
pollLast()
Retrieves and removes the last element of this deque, or returns null if
this deque is empty.
|
E |
pop() |
void |
push(E e) |
E |
remove() |
boolean |
remove(Object o)
Removes the first element e such that o.equals(e),
if such an element exists in this deque.
|
E |
removeFirst()
Removes and returns the first element from this deque.
|
boolean |
removeFirstOccurrence(Object o)
Removes the first element e such that o.equals(e),
if such an element exists in this deque.
|
E |
removeLast()
Removes and returns the last element from this deque.
|
boolean |
removeLastOccurrence(Object o)
Removes the last element e such that o.equals(e),
if such an element exists in this deque.
|
int |
size()
Returns the number of elements in this deque.
|
Object[] |
toArray()
Returns an array containing all of the elements in this deque in the
correct order.
|
<T> T[] |
toArray(T[] a)
Returns an array containing all of the elements in this deque in the
correct order; the runtime type of the returned array is that of the
specified array.
|
containsAll, removeAll, retainAll, toStringclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitequals, hashCode, parallelStream, removeIf, spliterator, streampublic ConcurrentDoublyLinkedList()
public ConcurrentDoublyLinkedList(Collection<? extends E> c)
c - the collection whose elements are to be placed into this
deque.NullPointerException - if c or any element within it is nullpublic ConcurrentDoublyLinkedList.Node addFirst(E o)
o - the element to be inserted at the beginning of this deque.NullPointerException - if the specified element is nullpublic ConcurrentDoublyLinkedList.Node addLast(E o)
o - the element to be inserted at the end of this deque.NullPointerException - if the specified element is nullpublic boolean offerFirst(E o)
o - the element to be inserted at the beginning of this deque.NullPointerException - if the specified element is nullpublic boolean offerLast(E o)
o - the element to be inserted at the end of this deque.NullPointerException - if the specified element is nullpublic E peekFirst()
public E peekLast()
public E getFirst()
NoSuchElementException - if this deque is empty.public E getLast()
NoSuchElementException - if this deque is empty.public E pollFirst()
public E pollLast()
public E removeFirst()
NoSuchElementException - if this deque is empty.public E removeLast()
NoSuchElementException - if this deque is empty.public boolean offer(E e)
public boolean add(E e)
add in interface Collection<E>add in class AbstractCollection<E>public E poll()
public E remove()
public E peek()
public E element()
public void push(E e)
public E pop()
public boolean removeFirstOccurrence(Object o)
o - element to be removed from this deque, if present.NullPointerException - if the specified element is nullpublic boolean removeLastOccurrence(Object o)
o - element to be removed from this deque, if present.NullPointerException - if the specified element is nullpublic boolean contains(Object o)
contains in interface Collection<E>contains in class AbstractCollection<E>o - element whose presence in this deque is to be tested.public boolean isEmpty()
isEmpty in interface Collection<E>isEmpty in class AbstractCollection<E>public int size()
Beware that, unlike in most collections, this method is NOT a constant-time operation. Because of the asynchronous nature of these deques, 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 class AbstractCollection<E>public boolean remove(Object o)
remove in interface Collection<E>remove in class AbstractCollection<E>o - element to be removed from this deque, if present.NullPointerException - if the specified element is nullpublic boolean addAll(Collection<? extends E> c)
addAll in interface Collection<E>addAll in class AbstractCollection<E>c - the elements to be inserted into this deque.NullPointerException - if c or any element within it is nullpublic void clear()
clear in interface Collection<E>clear in class AbstractCollection<E>public Object[] toArray()
toArray in interface Collection<E>toArray in class AbstractCollection<E>public <T> T[] toArray(T[] a)
If the deque fits in the specified array with room to spare (i.e., the array has more elements than the deque), the element in the array immediately following the end of the collection is set to null. This is useful in determining the length of the deque only if the caller knows that the deque does not contain any null elements.
toArray in interface Collection<E>toArray in class AbstractCollection<E>a - the array into which the elements of the deque are to be
stored, if it is big enough; otherwise, a new array of the
same runtime type is allocated for this purpose.ArrayStoreException - if the runtime type of a is not a supertype of the runtime
type of every element in this deque.NullPointerException - if the specified array is null.public Iterator<E> iterator()
ConcurrentModificationException, and may proceed concurrently
with other operations.iterator in interface Iterable<E>iterator in interface Collection<E>iterator in class AbstractCollection<E>Copyright © 2019 The Apache Software Foundation. All rights reserved.