![]() |
VOOZH | about |
In Java, the BlockingDeque interface is a part of the Java Collections Framework. It is an interface that extends Deque (double-ended queue) and provides methods for thread-safe operation. It allows threads to safely add and remove elements from both ends of the queue.
Example: This example demonstrates how to insert elements into a BlockingDeque and print its contents.
The elements in the BlockingDeque: [10, 20, 30, 40, 50]
This below diagram demonstrates the BlockingDeque Hierarchy:
In Java, the declaration of BlockingDeque can be done as:
BlockingDeque<Type> deque = new LinkedBlockingDeque<>();
Note: "Type" is the type of the element we want to store (e.g. Integer, String).
BlockingDeque is implemented by the class LinkedBlockingQueue, which uses a linked list to store elements. We can create a bounded or unbounded deque. If no capacity is specified, it defaults to Integer.MAX_VALUE.
To use BlockingDeque import it like this:
import java.util.concurrent.BlockingDeque;
or
import java.util.concurrent.*;
Example: This example demonstrates how to use LinkedBlockingQueue to add, iterate, remove an element, and print the contents of the deque.
The LinkedBlockingDeque contains: 134 245 23 122 90 The element 23 has been removed The LinkedBlockingDeque after remove operation contains: [134, 245, 122, 90]
1. Adding Elements: We can use add() to insert elements to the BlockingDeque.
Example: This example demonstrates how to add elements to a LinkedBlockingQueue and copy elements from one deque to another using addAll() method.
Contents of b1: [10, 20, 30, 40, 50] Contents of b2: [10, 20, 30, 40, 50]
2. Accessing Elements: We can use methods like contains(), element(), peek(), poll() to access the elements of BlockingDeque.
Example:
The LinkedBlockingDeque, b contains: [22, 125, 723, 172, 100] The LinkedBlockingDeque, b contains 22 The head of b: 22 The tail of b: 100
3. Removing Elements: We can use remove() method to remove elements from the BlockingDeque.
Example: This example demonstrates how to remove specific elements, and handle the removal of a non-existent element from a LinkedBlockingDeque.
The LinkedBlockingDeque, b contains: [75, 86, 13, 44, 10] Element 1 not found in the deque. After removal the LinkedBlockingDeque, b contains: [75, 13, 10]
4. Iterating Elements: We can use the iterator() method to iterate through the elements of LinkedBlockingDeque.
Example: This example demonstrates how to iterate over a LinkedBlockingDeque using an iterator to print its elements.
The LinkedBlockingDeque contains: 10 20 30 40 50
The BlockingDeque interface has a various method which has to be defined by every implementing class. A deque can be implemented as a queue and as a stack, therefore the BlockingDeque interface provides methods to do the same. The table below provides a list of all the methods and their function.
Methods | Description |
|---|---|
| boolean add(E element) | Adds the element to the queue represented by this deque (i.e. at the tail) if possible without violating any capacity restrictions. Returns true if the insertion is successful else throws an exception. |
| void addFirst(E element) | Adds the element at the head of the deque if possible without violating any capacity restrictions. If the operation is not successful then it throws an exception. |
| void addLast(E element) | Adds the element at the tail of the deque if possible without violating any capacity restrictions. If the operation is not successful then it throws an exception. |
| boolean contains(Object o) | Returns true if this deque contains the specified element. |
| E element() | Retrieves the head of the queue represented by this deque. |
| Iterator<E> iterator() | Returns an iterator over the elements in this deque in the proper sequence. |
| boolean offer(E element) | Inserts the element into the queue represented by this deque (i.e. at the tail) if possible immediately without violating any capacity restrictions. Returns true on successful insertion and false otherwise. |
| boolean offer(E element, long time, TimeUnit unit) | Inserts the element into the queue represented by this deque (i.e. at the tail) immediately if possible, else waits for the time specified for the space to become available. |
| boolean offerFirst(E element) | Inserts the element at the head of the deque if possible immediately without violating any capacity restrictions. Returns true on successful insertion and false otherwise. |
| boolean offerFirst(E element, long time, TimeUnit unit) | Inserts the element at the head of the deque immediately if possible, else waits for the time specified for the space to become available. |
| boolean offerLast(E element) | Inserts the element at the tail of the deque if possible immediately without violating any capacity restrictions. Returns true on successful insertion and false otherwise. |
| boolean offerLast(E element, long time, TimeUnit unit) | Inserts the element at the tail of the deque immediately if possible, else waits for the time specified for the space to become available. |
| E peek() | Returns the head of the queue represented by this deque if present else returns null. |
| E poll() | Retrieves and removes the head of the queue represented by this deque if present else returns null. |
| E poll(long time, TimeUnit unit) | Retrieves and removes the head of the queue represented by this deque. If no element is present then it waits for the specified time for it to become available. |
| E pollFirst(long time, TimeUnit unit) | Retrieves and removes the head of the deque. If no element is present then it waits for the specified time for it to become available. |
| E pollLast(long time, TimeUnit unit) | Retrieves and removes the tail of the deque. If no element is present then it waits for the specified time for it to become available. |
| void 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. |
| void 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. |
| void putFirst(E e) | Inserts the specified element at the front of this deque, waiting if necessary for space to become available. |
| void putLast(E e) | Inserts the specified element at the end of this deque, waiting if necessary for space to become available. |
| E remove() | Retrieves and removes the head of the queue represented by this deque. |
| boolean remove(Object obj) | Removes the first occurrence of the specified element from the deque. |
| boolean removeFirstOccurance(Object obj) | Removes the first occurrence of the specified element from the deque. |
| boolean removeLastOccurance(Object obj) | Removes the last occurrence of the specified element from the deque. |
| int size() | Returns the number of elements in this deque. |
| E take() | Retrieves and removes the head of the queue represented by this deque. If required waiting for the element to become available. |
| E takeFirst() | Retrieves and removes the head of the deque. If required waiting for the element to become available. |
| E takeLast() | Retrieves and removes the tail of the deque. If required waiting for the element to become available. |
Note: Here, E is the type of elements in the collection and TimeUnit is an enum that represents the time durations.
Methods | Description |
|---|---|
| drainTo(Collection<? super E> c) | Removes all available elements from this queue and adds them to the given collection. |
| drainTo(Collection<? super E> c, int maxElements) | Removes at most the given number of available elements from this queue and adds them to the given collection. |
| remainingCapacity() | Returns the number of additional elements that this queue can ideally (in the absence of memory or resource constraints) accept without blocking, or Integer.MAX_VALUE if there is no intrinsic limit. |
Method | Description |
|---|---|
| clear() | Removes all of the elements from this collection (optional operation). |
| containsAll(Collection<?> c) | Returns true if this collection contains all of the elements in the specified collection. |
| equals(Object o) | Compares the specified object with this collection for equality. |
| hashCode() | Returns the hash code value for this collection. |
| isEmpty() | Returns true if this collection contains no elements. |
| parallelStream() | Returns a possibly parallel Stream with this collection as its source. |
| removeAll(Collection<?> c) | Removes all of this collection's elements that are also contained in the specified collection (optional operation). |
| removeIf(Predicate<? super E> filter) | Removes all of the elements of this collection that satisfy the given predicate. |
| retainAll(Collection<?> c) | Retains only the elements in this collection that are contained in the specified collection (optional operation). |
| spliterator() | Creates a Spliterator over the elements in this collection. |
| stream() | Returns a sequential Stream with this collection as its source. |
| toArray() | Returns an array containing all of the elements in this collection. |
| toArray(IntFunction<T[]> generator) | Returns an array containing all of the elements in this collection, using the provided generator function to allocate the returned array. |
| toArray(T[] a) | Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. |
Method | Description |
|---|---|
| addAll(Collection<? extends E> c) | Adds all of the elements in the specified collection at the end of this deque, as if by calling addLast(E) on each one, in the order that they are returned by the collection's iterator. |
| descendingIterator() | Returns an iterator over the elements in this deque in reverse sequential order. |
| getFirst() | Retrieves, but does not remove, the first element of this deque. |
| getLast() | Retrieves, but does not remove, the last element of this deque. |
| peekFirst() | Retrieves, but does not remove, the first element of this deque, or returns null if this deque is empty. |
| peekLast() | Retrieves, but does not remove, the last element of this deque, or returns null if this deque is empty. |
| pollFirst() | Retrieves and removes the first element of this deque, or returns null if this deque is empty. |
| pollLast() | Retrieves and removes the last element of this deque, or returns null if this deque is empty. |
| pop() | Pops an element from the stack represented by this deque. |
| removeFirst() | Retrieves and removes the first element of this deque. |
| removeLast() | Retrieves and removes the last element of this deque. |
Method | Description |
|---|---|
| forEach(Consumer<? super T> action) | Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception. |
The following are the methods provided by the BlockingDeque for insertion, removal, and examine operations on the deque. Each of the four sets of methods behaves differently if the requested operation is not satisfied immediately.
Operation | Throw Exception | Special value | Blocks | Times out |
|---|---|---|---|---|
Insert | offerFirst(e, timeout, timeunit) | |||
Remove | removeFirst() | pollFirst() | pollFirst(timeout, timeunit) | |
Examine | getFirst() | peekFirst() | not applicable | pollFirst(timeout, timeunit) |
Operation | Throw Exception | Special value | Blocks | Times out |
|---|---|---|---|---|
Insert | putLast(e) | offerLast(e, timeout, timeunit) | ||
Remove | removeLast() | pollLast() | pollLast(timeout, timeunit) | |
Examine | getLast() | peekLast() | not applicable | not applicable |
We know that we can insert, remove, and examine elements from both directions in the BlockingDeque. Since BlockingDeque extends BlockingQueue, the methods for insertion, removal, and examination operations acting on a direction on BlockingDeque are similar to BlockingQueue. The following comparison explains the same.
Operation | BlockingQueue Method | Equivalent BlockingDeque Method |
|---|---|---|
| Insert | add(e) | addLast(e) |
| offer(e) | 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() |