Added in API level 9

BlockingDeque

interface BlockingDeque<E : Any!> : BlockingQueue<E>, Deque<E>
java.util.concurrent.BlockingDeque

A Deque that additionally supports blocking operations that wait for the deque to become non-empty when retrieving an element, and wait for space to become available in the deque when storing an element.

BlockingDeque methods come in four forms, with different ways of handling operations that cannot be satisfied immediately, but may be satisfied at some point in the future: one throws an exception, the second returns a special value (either null or false, depending on the operation), the third blocks the current thread indefinitely until the operation can succeed, and the fourth blocks for only a given maximum time limit before giving up. These methods are summarized in the following table:

Summary of BlockingDeque methods
First Element (Head)
Throws exception Special value Blocks Times out
Insert addFirst(java.lang.Object) offerFirst(e) putFirst(e) offerFirst(e, time, unit)
Remove removeFirst() pollFirst() takeFirst() pollFirst(time, unit)
Examine getFirst() peekFirst() not applicable not applicable
Last Element (Tail)
Throws exception Special value Blocks Times out
Insert addLast(java.lang.Object) offerLast(e) putLast(e) offerLast(e, time, unit)
Remove removeLast() pollLast() takeLast() pollLast(time, unit)
Examine getLast() peekLast() not applicable not applicable

Like any BlockingQueue, a BlockingDeque is thread safe, does not permit null elements, and may (or may not) be capacity-constrained.

A BlockingDeque implementation may be used directly as a FIFO BlockingQueue. The methods inherited from the BlockingQueue interface are precisely equivalent to BlockingDeque methods as indicated in the following table:

Comparison of BlockingQueue and BlockingDeque methods
BlockingQueue Method Equivalent BlockingDeque Method
Insert add(java.lang.Object) addLast(java.lang.Object)
offer(java.lang.Object) offerLast(e)
put(e) putLast(e)
offer(e, time, unit) offerLast(e, time, unit)
Remove remove() removeFirst()
poll() pollFirst()
take() takeFirst()
poll(time, unit) pollFirst(time, unit)
Examine element() getFirst()
peek() peekFirst()

Memory consistency effects: As with other concurrent collections, actions in a thread prior to placing an object into a BlockingDeque happen-before actions subsequent to the access or removal of that element from the BlockingDeque in another thread.

This interface is a member of the Java Collections Framework.

Summary

Public methods
abstract Boolean
add(element: E)

Inserts the specified element into the queue represented by this deque (in other words, at the tail of this deque) if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available.

abstract Unit
addFirst(e: E)

Inserts the specified element at the front of this deque if it is possible to do so immediately without violating capacity restrictions, throwing an IllegalStateException if no space is currently available.

abstract Unit
addLast(e: E)

Inserts the specified element at the end of this deque if it is possible to do so immediately without violating capacity restrictions, throwing an IllegalStateException if no space is currently available.

abstract Boolean
contains(element: E?)

Returns true if this deque contains the specified element.

abstract E

Retrieves, but does not remove, the head of the queue represented by this deque (in other words, the first element of this deque).

abstract MutableIterator<E>

Returns an iterator over the elements in this deque in proper sequence.

abstract Boolean
offer(e: E)

Inserts the specified element into the queue represented by this deque (in other words, at the tail of this deque) if it is possible to do so immediately without violating capacity restrictions, returning true upon success and false if no space is currently available.

abstract Boolean
offer(e: E, timeout: Long, unit: TimeUnit!)

Inserts the specified element into the queue represented by this deque (in other words, at the tail of this deque), waiting up to the specified wait time if necessary for space to become available.

abstract Boolean
offerFirst(e: E)

Inserts the specified element at the front of this deque if it is possible to do so immediately without violating capacity restrictions, returning true upon success and false if no space is currently available.

abstract Boolean
offerFirst(e: E, timeout: Long, unit: TimeUnit!)

Inserts the specified element at the front of this deque, waiting up to the specified wait time if necessary for space to become available.

abstract Boolean
offerLast(e: E)

Inserts the specified element at the end of this deque if it is possible to do so immediately without violating capacity restrictions, returning true upon success and false if no space is currently available.

abstract Boolean
offerLast(e: E, timeout: Long, unit: TimeUnit!)

Inserts the specified element at the end of this deque, waiting up to the specified wait time if necessary for space to become available.

abstract E?

Retrieves, but does not remove, the head of the queue represented by this deque (in other words, the first element of this deque), or returns null if this deque is empty.

abstract E?

Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque), or returns null if this deque is empty.

abstract E
poll(timeout: Long, unit: TimeUnit!)

Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque), waiting up to the specified wait time if necessary for an element to become available.

abstract E
pollFirst(timeout: Long, unit: TimeUnit!)

Retrieves and removes the first element of this deque, waiting up to the specified wait time if necessary for an element to become available.

abstract E
pollLast(timeout: Long, unit: TimeUnit!)

Retrieves and removes the last element of this deque, waiting up to the specified wait time if necessary for an element to become available.

abstract Unit
push(e: E)

Pushes an element onto the stack represented by this deque (in other words, at the head of this deque) if it is possible to do so immediately without violating capacity restrictions, throwing an IllegalStateException if no space is currently available.

abstract Unit
put(e: E)

Inserts the specified element into the queue represented by this deque (in other words, at the tail of this deque), waiting if necessary for space to become available.

abstract Unit
putFirst(e: E)

Inserts the specified element at the front of this deque, waiting if necessary for space to become available.

abstract Unit
putLast(e: E)

Inserts the specified element at the end of this deque, waiting if necessary for space to become available.

abstract E

Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque).

abstract Boolean
remove(element: E?)

Removes the first occurrence of the specified element from this deque.

abstract Boolean

Removes the first occurrence of the specified element from this deque.

abstract Boolean

Removes the last occurrence of the specified element from this deque.

abstract E

Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque), waiting if necessary until an element becomes available.

abstract E

Retrieves and removes the first element of this deque, waiting if necessary until an element becomes available.

abstract E

Retrieves and removes the last element of this deque, waiting if necessary until an element becomes available.

Inherited functions
Properties
abstract Int

Returns the number of elements in this deque.

Public methods

add

Added in API level 9
abstract fun add(element: E): Boolean

Inserts the specified element into the queue represented by this deque (in other words, at the tail of this deque) if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available. When using a capacity-restricted deque, it is generally preferable to use #offer(java.lang.Object).

This method is equivalent to #addLast(java.lang.Object).

Parameters
e the element to add
Return
Boolean true (as specified by Collection#add)
Exceptions
java.lang.UnsupportedOperationException if the add operation is not supported by this collection
java.lang.ClassCastException if the class of the specified element prevents it from being added to this deque
java.lang.NullPointerException if the specified element is null
java.lang.IllegalArgumentException if some property of the specified element prevents it from being added to this deque
java.lang.IllegalStateException if the element cannot be added at this time due to capacity restrictions

addFirst

Added in API level 9
abstract fun addFirst(e: E): Unit

Inserts the specified element at the front of this deque if it is possible to do so immediately without violating capacity restrictions, throwing an IllegalStateException if no space is currently available. When using a capacity-restricted deque, it is generally preferable to use offerFirst.

Parameters
e E: the element to add
Exceptions
java.lang.NullPointerException if the specified element is null
java.lang.UnsupportedOperationException if this collection implementation does not support this operation
java.lang.IllegalStateException if the element cannot be added at this time due to capacity restrictions
java.lang.ClassCastException if the class of the specified element prevents it from being added to this deque
java.lang.IllegalArgumentException if some property of the specified element prevents it from being added to this deque

addLast

Added in API level 9
abstract fun addLast(e: E): Unit

Inserts the specified element at the end of this deque if it is possible to do so immediately without violating capacity restrictions, throwing an IllegalStateException if no space is currently available. When using a capacity-restricted deque, it is generally preferable to use offerLast.

Parameters
e E: the element to add
Exceptions
java.lang.NullPointerException if the specified element is null
java.lang.UnsupportedOperationException if this collection implementation does not support this operation
java.lang.IllegalStateException if the element cannot be added at this time due to capacity restrictions
java.lang.ClassCastException if the class of the specified element prevents it from being added to this deque
java.lang.IllegalArgumentException if some property of the specified element prevents it from being added to this deque

contains

Added in API level 9
abstract fun contains(element: E?): Boolean

Returns 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).

Parameters
o object to be checked for containment in this deque
Return
Boolean true if this deque contains the specified element
Exceptions
java.lang.ClassCastException if the class of the specified element is incompatible with this deque (optional)
java.lang.NullPointerException if the specified element is null (optional)

element

Added in API level 9
abstract fun element(): E

Retrieves, but does not remove, the head of the queue represented by this deque (in other words, the first element of this deque). This method differs from peek only in that it throws an exception if this deque is empty.

This method is equivalent to getFirst.

Return
E the head of this deque
Exceptions
java.util.NoSuchElementException if this deque is empty

iterator

Added in API level 9
abstract fun iterator(): MutableIterator<E>

Returns an iterator over the elements in this deque in proper sequence. The elements will be returned in order from first (head) to last (tail).

Return
MutableIterator<E> an iterator over the elements in this deque in proper sequence

offer

Added in API level 9
abstract fun offer(e: E): Boolean

Inserts the specified element into the queue represented by this deque (in other words, at the tail of this deque) if it is possible to do so immediately without violating capacity restrictions, returning true upon success and false if no space is currently available. When using a capacity-restricted deque, this method is generally preferable to the #add method, which can fail to insert an element only by throwing an exception.

This method is equivalent to offerLast.

Parameters
e E: the element to add
Return
Boolean true if the element was added to this deque, else false
Exceptions
java.lang.ClassCastException if the class of the specified element prevents it from being added to this deque
java.lang.NullPointerException if the specified element is null
java.lang.IllegalArgumentException if some property of the specified element prevents it from being added to this deque

offer

Added in API level 9
abstract fun offer(
    e: E,
    timeout: Long,
    unit: TimeUnit!
): Boolean

Inserts the specified element into the queue represented by this deque (in other words, at the tail of this deque), waiting up to the specified wait time if necessary for space to become available.

This method is equivalent to offerLast.

Parameters
e E: the element to add
timeout Long: how long to wait before giving up, in units of unit
unit TimeUnit!: a TimeUnit determining how to interpret the timeout parameter
Return
Boolean true if the element was added to this deque, else false
Exceptions
java.lang.InterruptedException if interrupted while waiting
java.lang.ClassCastException if the class of the specified element prevents it from being added to this deque
java.lang.NullPointerException if the specified element is null
java.lang.IllegalArgumentException if some property of the specified element prevents it from being added to this deque

offerFirst

Added in API level 9
abstract fun offerFirst(e: E): Boolean

Inserts the specified element at the front of this deque if it is possible to do so immediately without violating capacity restrictions, returning true upon success and false if no space is currently available. When using a capacity-restricted deque, this method is generally preferable to the #addFirst(java.lang.Object) method, which can fail to insert an element only by throwing an exception.

Parameters
e E: the element to add
Return
Boolean true if the element was added to this deque, else false
Exceptions
java.lang.ClassCastException if the class of the specified element prevents it from being added to this deque
java.lang.NullPointerException if the specified element is null
java.lang.IllegalArgumentException if some property of the specified element prevents it from being added to this deque

offerFirst

Added in API level 9
abstract fun offerFirst(
    e: E,
    timeout: Long,
    unit: TimeUnit!
): Boolean

Inserts the specified element at the front of this deque, waiting up to the specified wait time if necessary for space to become available.

Parameters
e E: the element to add
timeout Long: how long to wait before giving up, in units of unit
unit TimeUnit!: a TimeUnit determining how to interpret the timeout parameter
Return
Boolean true if successful, or false if the specified waiting time elapses before space is available
Exceptions
java.lang.InterruptedException if interrupted while waiting
java.lang.ClassCastException if the class of the specified element prevents it from being added to this deque
java.lang.NullPointerException if the specified element is null
java.lang.IllegalArgumentException if some property of the specified element prevents it from being added to this deque

offerLast

Added in API level 9
abstract fun offerLast(e: E): Boolean

Inserts the specified element at the end of this deque if it is possible to do so immediately without violating capacity restrictions, returning true upon success and false if no space is currently available. When using a capacity-restricted deque, this method is generally preferable to the #addLast(java.lang.Object) method, which can fail to insert an element only by throwing an exception.

Parameters
e E: the element to add
Return
Boolean true if the element was added to this deque, else false
Exceptions
java.lang.ClassCastException if the class of the specified element prevents it from being added to this deque
java.lang.NullPointerException if the specified element is null
java.lang.IllegalArgumentException if some property of the specified element prevents it from being added to this deque

offerLast

Added in API level 9
abstract fun offerLast(
    e: E,
    timeout: Long,
    unit: TimeUnit!
): Boolean

Inserts the specified element at the end of this deque, waiting up to the specified wait time if necessary for space to become available.

Parameters
e E: the element to add
timeout Long: how long to wait before giving up, in units of unit
unit TimeUnit!: a TimeUnit determining how to interpret the timeout parameter
Return
Boolean true if successful, or false if the specified waiting time elapses before space is available
Exceptions
java.lang.InterruptedException if interrupted while waiting
java.lang.ClassCastException if the class of the specified element prevents it from being added to this deque
java.lang.NullPointerException if the specified element is null
java.lang.IllegalArgumentException if some property of the specified element prevents it from being added to this deque

peek

Added in API level 9
abstract fun peek(): E?

Retrieves, but does not remove, the head of the queue represented by this deque (in other words, the first element of this deque), or returns null if this deque is empty.

This method is equivalent to peekFirst.

Return
E? the head of this deque, or null if this deque is empty

poll

Added in API level 9
abstract fun poll(): E?

Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque), or returns null if this deque is empty.

This method is equivalent to pollFirst().

Return
E? the head of this deque, or null if this deque is empty

poll

Added in API level 9
abstract fun poll(
    timeout: Long,
    unit: TimeUnit!
): E

Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque), waiting up to the specified wait time if necessary for an element to become available.

This method is equivalent to pollFirst.

Parameters
timeout Long: how long to wait before giving up, in units of unit
unit TimeUnit!: a TimeUnit determining how to interpret the timeout parameter
Return
E the head of this deque, or null if the specified waiting time elapses before an element is available
Exceptions
java.lang.InterruptedException if interrupted while waiting

pollFirst

Added in API level 9
abstract fun pollFirst(
    timeout: Long,
    unit: TimeUnit!
): E

Retrieves and removes the first element of this deque, waiting up to the specified wait time if necessary for an element to become available.

Parameters
timeout Long: how long to wait before giving up, in units of unit
unit TimeUnit!: a TimeUnit determining how to interpret the timeout parameter
Return
E the head of this deque, or null if the specified waiting time elapses before an element is available
Exceptions
java.lang.InterruptedException if interrupted while waiting

pollLast

Added in API level 9
abstract fun pollLast(
    timeout: Long,
    unit: TimeUnit!
): E

Retrieves and removes the last element of this deque, waiting up to the specified wait time if necessary for an element to become available.

Parameters
timeout Long: how long to wait before giving up, in units of unit
unit TimeUnit!: a TimeUnit determining how to interpret the timeout parameter
Return
E the tail of this deque, or null if the specified waiting time elapses before an element is available
Exceptions
java.lang.InterruptedException if interrupted while waiting

push

Added in API level 9
abstract fun push(e: E): Unit

Pushes an element onto the stack represented by this deque (in other words, at the head of this deque) if it is possible to do so immediately without violating capacity restrictions, throwing an IllegalStateException if no space is currently available.

This method is equivalent to #addFirst(java.lang.Object).

Parameters
e E: the element to push
Exceptions
java.lang.IllegalStateException if the element cannot be added at this time due to capacity restrictions
java.lang.ClassCastException if the class of the specified element prevents it from being added to this deque
java.lang.NullPointerException if the specified element is null
java.lang.IllegalArgumentException if some property of the specified element prevents it from being added to this deque

put

Added in API level 9
abstract fun put(e: E): Unit

Inserts the specified element into the queue represented by this deque (in other words, at the tail of this deque), waiting if necessary for space to become available.

This method is equivalent to putLast.

Parameters
e E: the element to add
Exceptions
java.lang.InterruptedException if interrupted while waiting
java.lang.ClassCastException if the class of the specified element prevents it from being added to this deque
java.lang.NullPointerException if the specified element is null
java.lang.IllegalArgumentException if some property of the specified element prevents it from being added to this deque

putFirst

Added in API level 9
abstract fun putFirst(e: E): Unit

Inserts the specified element at the front of this deque, waiting if necessary for space to become available.

Parameters
e E: the element to add
Exceptions
java.lang.InterruptedException if interrupted while waiting
java.lang.ClassCastException if the class of the specified element prevents it from being added to this deque
java.lang.NullPointerException if the specified element is null
java.lang.IllegalArgumentException if some property of the specified element prevents it from being added to this deque

putLast

Added in API level 9
abstract fun putLast(e: E): Unit

Inserts the specified element at the end of this deque, waiting if necessary for space to become available.

Parameters
e E: the element to add
Exceptions
java.lang.InterruptedException if interrupted while waiting
java.lang.ClassCastException if the class of the specified element prevents it from being added to this deque
java.lang.NullPointerException if the specified element is null
java.lang.IllegalArgumentException if some property of the specified element prevents it from being added to this deque

remove

Added in API level 9
abstract fun remove(): E

Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque). This method differs from poll() only in that it throws an exception if this deque is empty.

This method is equivalent to removeFirst.

Return
E the head of the queue represented by this deque
Exceptions
java.util.NoSuchElementException if this deque is empty

remove

Added in API level 9
abstract fun remove(element: E?): Boolean

Removes the first occurrence of the specified element from this deque. If the deque does not contain the element, it is unchanged. More formally, removes the first element 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.

Parameters
o element to be removed from this deque, if present
Return
Boolean true if this deque changed as a result of the call
Exceptions
java.lang.ClassCastException if the class of the specified element is incompatible with this deque (optional)
java.lang.NullPointerException if the specified element is null (optional)
java.lang.UnsupportedOperationException if the remove operation is not supported by this collection

removeFirstOccurrence

Added in API level 9
abstract fun removeFirstOccurrence(o: Any?): Boolean

Removes the first occurrence of the specified element from this deque. If the deque does not contain the element, it is unchanged. More formally, removes the first element 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).

Parameters
o Any?: element to be removed from this deque, if present
Return
Boolean true if an element was removed as a result of this call
Exceptions
java.lang.ClassCastException if the class of the specified element is incompatible with this deque (optional)
java.lang.NullPointerException if the specified element is null (optional)

removeLastOccurrence

Added in API level 9
abstract fun removeLastOccurrence(o: Any?): Boolean

Removes the last occurrence of the specified element from this deque. If the deque does not contain the element, it is unchanged. More formally, removes the last element 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).

Parameters
o Any?: element to be removed from this deque, if present
Return
Boolean true if an element was removed as a result of this call
Exceptions
java.lang.ClassCastException if the class of the specified element is incompatible with this deque (optional)
java.lang.NullPointerException if the specified element is null (optional)

take

Added in API level 9
abstract fun take(): E

Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque), waiting if necessary until an element becomes available.

This method is equivalent to takeFirst.

Return
E the head of this deque
Exceptions
java.lang.InterruptedException if interrupted while waiting

takeFirst

Added in API level 9
abstract fun takeFirst(): E

Retrieves and removes the first element of this deque, waiting if necessary until an element becomes available.

Return
E the head of this deque
Exceptions
java.lang.InterruptedException if interrupted while waiting

takeLast

Added in API level 9
abstract fun takeLast(): E

Retrieves and removes the last element of this deque, waiting if necessary until an element becomes available.

Return
E the tail of this deque
Exceptions
java.lang.InterruptedException if interrupted while waiting

Properties

size

Added in API level 9
abstract val size: Int

Returns the number of elements in this deque.

Return
Int the number of elements in this deque