E
- the type of elements held in this collectionpublic class LinkedBlockingDeque<E> extends AbstractQueue<E> implements BlockingDeque<E>, Serializable
The optional capacity bound constructor argument serves as a
way to prevent excessive expansion. The capacity, if unspecified,
is equal to Integer.MAX_VALUE
. Linked nodes are
dynamically created upon each insertion unless this would bring the
deque above capacity.
Most operations run in constant time (ignoring time spent
blocking). Exceptions include remove
,
removeFirstOccurrence
, removeLastOccurrence
, contains
, iterator.remove()
, and the bulk
operations, all of which run in linear time.
This class and its iterator implement all of the
optional methods of the Collection
and Iterator
interfaces.
This class is a member of the Java Collections Framework.
Constructor and Description |
---|
LinkedBlockingDeque()
Creates a
LinkedBlockingDeque with a capacity of
Integer.MAX_VALUE . |
LinkedBlockingDeque(Collection<? extends E> c)
Creates a
LinkedBlockingDeque with a capacity of
Integer.MAX_VALUE , initially containing the elements of
the given collection, added in traversal order of the
collection's iterator. |
LinkedBlockingDeque(int capacity)
Creates a
LinkedBlockingDeque with the given (fixed) capacity. |
LinkedBlockingDeque(int capacity,
ReentrantLock lock)
Creates a
LinkedBlockingDeque with the given (fixed) capacity and
the caller's ReentrantLock object. |
LinkedBlockingDeque(ReentrantLock lock)
Creates a
LinkedBlockingDeque with a capacity of
Integer.MAX_VALUE using the caller's lock. |
Modifier and Type | Method and Description |
---|---|
boolean |
add(E e)
Inserts the specified element at the end of this deque unless it would
violate capacity restrictions.
|
void |
addFirst(E e) |
void |
addLast(E e) |
void |
clear()
Atomically removes all of the elements from this deque.
|
boolean |
contains(Object o)
Returns
true if this deque contains the specified element. |
Iterator<E> |
descendingIterator()
Returns an iterator over the elements in this deque in reverse
sequential order.
|
int |
drainTo(Collection<? super E> c) |
int |
drainTo(Collection<? super E> c,
int maxElements) |
E |
element()
Retrieves, but does not remove, the head of the queue represented by
this deque.
|
E |
getFirst() |
E |
getLast() |
Iterator<E> |
iterator()
Returns an iterator over the elements in this deque in proper sequence.
|
boolean |
offer(E e) |
boolean |
offer(E e,
long timeout,
TimeUnit unit) |
boolean |
offerFirst(E e) |
boolean |
offerFirst(E e,
long timeout,
TimeUnit unit) |
boolean |
offerLast(E e) |
boolean |
offerLast(E e,
long timeout,
TimeUnit unit) |
E |
peek() |
E |
peekFirst() |
E |
peekLast() |
E |
poll() |
E |
poll(long timeout,
TimeUnit unit) |
E |
pollFirst() |
E |
pollFirst(long timeout,
TimeUnit unit) |
E |
pollLast() |
E |
pollLast(long timeout,
TimeUnit unit) |
E |
pop() |
void |
push(E e) |
void |
put(E e) |
void |
putFirst(E e) |
void |
putLast(E e) |
int |
remainingCapacity()
Returns the number of additional elements that this deque can ideally
(in the absence of memory or resource constraints) accept without
blocking.
|
E |
remove()
Retrieves and removes the head of the queue represented by this deque.
|
boolean |
remove(Object o)
Removes the first occurrence of the specified element from this deque.
|
E |
removeFirst() |
boolean |
removeFirstOccurrence(Object o) |
E |
removeLast() |
boolean |
removeLastOccurrence(Object o) |
int |
size()
Returns the number of elements in this deque.
|
E |
take() |
E |
takeFirst() |
E |
takeLast() |
Object[] |
toArray()
Returns an array containing all of the elements in this deque, in
proper sequence (from first to last element).
|
<T> T[] |
toArray(T[] a)
Returns an array containing all of the elements in this deque, in
proper sequence; the runtime type of the returned array is that of
the specified array.
|
String |
toString() |
addAll
containsAll, isEmpty, removeAll, retainAll
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
addAll, containsAll, equals, hashCode, isEmpty, removeAll, retainAll
public LinkedBlockingDeque()
LinkedBlockingDeque
with a capacity of
Integer.MAX_VALUE
.public LinkedBlockingDeque(ReentrantLock lock)
LinkedBlockingDeque
with a capacity of
Integer.MAX_VALUE
using the caller's lock.public LinkedBlockingDeque(int capacity)
LinkedBlockingDeque
with the given (fixed) capacity.capacity
- the capacity of this dequeIllegalArgumentException
- if capacity
is less than 1public LinkedBlockingDeque(int capacity, ReentrantLock lock)
LinkedBlockingDeque
with the given (fixed) capacity and
the caller's ReentrantLock
object.
Caution: By using the caller's lock, this constructor allows the caller to break the encapsulation of the synchronization and lock-based notification (signals). This can be used advantageously to create designs where an outer lock is shared by the collection which avoid deadlock arising from blocking operations on an inner lock while holding a distinct outer lock. However, the caller's decisions about its lock are no longer independent of the design decisions within this class since they share the same lock.
capacity
- the capacity of this dequelock
- the lock object.IllegalArgumentException
- if capacity
is less than 1public LinkedBlockingDeque(Collection<? extends E> c)
LinkedBlockingDeque
with a capacity of
Integer.MAX_VALUE
, initially containing the elements of
the given collection, added in traversal order of the
collection's iterator.c
- the collection of elements to initially containNullPointerException
- if the specified collection or any
of its elements are nullpublic void addFirst(E e)
addFirst
in interface BlockingDeque<E>
addFirst
in interface Deque<E>
IllegalStateException
NullPointerException
public void addLast(E e)
addLast
in interface BlockingDeque<E>
addLast
in interface Deque<E>
IllegalStateException
NullPointerException
public boolean offerFirst(E e)
offerFirst
in interface BlockingDeque<E>
offerFirst
in interface Deque<E>
NullPointerException
public boolean offerLast(E e)
offerLast
in interface BlockingDeque<E>
offerLast
in interface Deque<E>
NullPointerException
public void putFirst(E e) throws InterruptedException
putFirst
in interface BlockingDeque<E>
NullPointerException
InterruptedException
public void putLast(E e) throws InterruptedException
putLast
in interface BlockingDeque<E>
NullPointerException
InterruptedException
public boolean offerFirst(E e, long timeout, TimeUnit unit) throws InterruptedException
offerFirst
in interface BlockingDeque<E>
NullPointerException
InterruptedException
public boolean offerLast(E e, long timeout, TimeUnit unit) throws InterruptedException
offerLast
in interface BlockingDeque<E>
NullPointerException
InterruptedException
public E removeFirst()
removeFirst
in interface Deque<E>
NoSuchElementException
public E removeLast()
removeLast
in interface Deque<E>
NoSuchElementException
public E takeFirst() throws InterruptedException
takeFirst
in interface BlockingDeque<E>
InterruptedException
public E takeLast() throws InterruptedException
takeLast
in interface BlockingDeque<E>
InterruptedException
public E pollFirst(long timeout, TimeUnit unit) throws InterruptedException
pollFirst
in interface BlockingDeque<E>
InterruptedException
public E pollLast(long timeout, TimeUnit unit) throws InterruptedException
pollLast
in interface BlockingDeque<E>
InterruptedException
public E getFirst()
getFirst
in interface Deque<E>
NoSuchElementException
public E getLast()
getLast
in interface Deque<E>
NoSuchElementException
public boolean removeFirstOccurrence(Object o)
removeFirstOccurrence
in interface BlockingDeque<E>
removeFirstOccurrence
in interface Deque<E>
public boolean removeLastOccurrence(Object o)
removeLastOccurrence
in interface BlockingDeque<E>
removeLastOccurrence
in interface Deque<E>
public boolean add(E e)
offer
.
This method is equivalent to addLast(E)
.
add
in interface Collection<E>
add
in interface BlockingDeque<E>
add
in interface BlockingQueue<E>
add
in interface Deque<E>
add
in interface Queue<E>
add
in class AbstractQueue<E>
IllegalStateException
- if the element cannot be added at this
time due to capacity restrictionsNullPointerException
- if the specified element is nullpublic boolean offer(E e)
offer
in interface BlockingDeque<E>
offer
in interface BlockingQueue<E>
offer
in interface Deque<E>
offer
in interface Queue<E>
NullPointerException
- if the specified element is nullpublic void put(E e) throws InterruptedException
put
in interface BlockingDeque<E>
put
in interface BlockingQueue<E>
NullPointerException
InterruptedException
public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException
offer
in interface BlockingDeque<E>
offer
in interface BlockingQueue<E>
NullPointerException
InterruptedException
public E remove()
poll
only in that it throws an
exception if this deque is empty.
This method is equivalent to removeFirst
.
remove
in interface BlockingDeque<E>
remove
in interface Deque<E>
remove
in interface Queue<E>
remove
in class AbstractQueue<E>
NoSuchElementException
- if this deque is emptypublic E poll()
public E take() throws InterruptedException
take
in interface BlockingDeque<E>
take
in interface BlockingQueue<E>
InterruptedException
public E poll(long timeout, TimeUnit unit) throws InterruptedException
poll
in interface BlockingDeque<E>
poll
in interface BlockingQueue<E>
InterruptedException
public E element()
peek
only in that
it throws an exception if this deque is empty.
This method is equivalent to getFirst
.
element
in interface BlockingDeque<E>
element
in interface Deque<E>
element
in interface Queue<E>
element
in class AbstractQueue<E>
NoSuchElementException
- if this deque is emptypublic E peek()
public int remainingCapacity()
size
of this deque.
Note that you cannot always tell if an attempt to insert
an element will succeed by inspecting remainingCapacity
because it may be the case that another thread is about to
insert or remove an element.
remainingCapacity
in interface BlockingQueue<E>
public int drainTo(Collection<? super E> c)
drainTo
in interface BlockingQueue<E>
UnsupportedOperationException
ClassCastException
NullPointerException
IllegalArgumentException
public int drainTo(Collection<? super E> c, int maxElements)
drainTo
in interface BlockingQueue<E>
UnsupportedOperationException
ClassCastException
NullPointerException
IllegalArgumentException
public void push(E e)
push
in interface BlockingDeque<E>
push
in interface Deque<E>
IllegalStateException
NullPointerException
public E pop()
pop
in interface Deque<E>
NoSuchElementException
public boolean remove(Object o)
e
such that
o.equals(e)
(if such an element exists).
Returns true
if this deque contained the specified element
(or equivalently, if this deque changed as a result of the call).
This method is equivalent to
removeFirstOccurrence
.
remove
in interface Collection<E>
remove
in interface BlockingDeque<E>
remove
in interface BlockingQueue<E>
remove
in interface Deque<E>
remove
in class AbstractCollection<E>
o
- element to be removed from this deque, if presenttrue
if this deque changed as a result of the callpublic int size()
size
in interface Collection<E>
size
in interface BlockingDeque<E>
size
in interface Deque<E>
size
in class AbstractCollection<E>
public boolean contains(Object o)
true
if this deque contains the specified element.
More formally, returns true
if and only if this deque contains
at least one element e
such that o.equals(e)
.contains
in interface Collection<E>
contains
in interface BlockingDeque<E>
contains
in interface BlockingQueue<E>
contains
in interface Deque<E>
contains
in class AbstractCollection<E>
o
- object to be checked for containment in this dequetrue
if this deque contains the specified elementpublic Object[] toArray()
The returned array will be "safe" in that no references to it are maintained by this deque. (In other words, this method must allocate a new array). The caller is thus free to modify the returned array.
This method acts as bridge between array-based and collection-based APIs.
toArray
in interface Collection<E>
toArray
in class AbstractCollection<E>
public <T> T[] toArray(T[] a)
If this deque fits in the specified array with room to spare
(i.e., the array has more elements than this deque), the element in
the array immediately following the end of the deque is set to
null
.
Like the toArray()
method, this method acts as bridge between
array-based and collection-based APIs. Further, this method allows
precise control over the runtime type of the output array, and may,
under certain circumstances, be used to save allocation costs.
Suppose x
is a deque known to contain only strings.
The following code can be used to dump the deque into a newly
allocated array of String
:
String[] y = x.toArray(new String[0]);Note that
toArray(new Object[0])
is identical in function to
toArray()
.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 purposeArrayStoreException
- if the runtime type of the specified array
is not a supertype of the runtime type of every element in
this dequeNullPointerException
- if the specified array is nullpublic String toString()
toString
in class AbstractCollection<E>
public void clear()
clear
in interface Collection<E>
clear
in class AbstractQueue<E>
public Iterator<E> iterator()
The returned iterator is a "weakly consistent" iterator that
will never throw ConcurrentModificationException
, and guarantees to traverse
elements as they existed upon construction of the iterator, and
may (but is not guaranteed to) reflect any modifications
subsequent to construction.
iterator
in interface Iterable<E>
iterator
in interface Collection<E>
iterator
in interface BlockingDeque<E>
iterator
in interface Deque<E>
iterator
in class AbstractCollection<E>
public Iterator<E> descendingIterator()
The returned iterator is a "weakly consistent" iterator that
will never throw ConcurrentModificationException
, and guarantees to traverse
elements as they existed upon construction of the iterator, and
may (but is not guaranteed to) reflect any modifications
subsequent to construction.
descendingIterator
in interface Deque<E>
Copyright © 2006–2019 SYSTAP, LLC DBA Blazegraph. All rights reserved.