This tutorial covers BlockingDeque in Java, its implementation classes, methods, and example of using BlockingDeque.
Table of Contents
Java BlockingDeque
BlockingDeque is an interface in Java that is part of the Collections framework and present in the java.util.concurrent
package. It blocks the insertion operation when the deque is full and blocks removal operation when it is empty. Since it is a Deque, it supports the insertion and removal of elements from both ends.
Hierarchy
Implementation class of BlockingDeque
The class that implements the Java BlockingDeque interface is the LinkedBlockingDeque
class. It internally has a LinkedList data structure representation. It may be bounded if we specify the capacity in the constructor else it points to Integer.MAX_VALUE.
BlockingDeque bq = new LinkedBlockingDeque();
Methods in Java BlockingDeque
Below are the methods of the BlockingDeque interface. It also imports the methods present in the Deque and Collections interface.
Method | Description | Parameter |
---|---|---|
Boolean add(Element e) | Adds the specified element to the end of the deque. | e - the element to be added. Return value - True |
Boolean addAll(Collection c) | Adds a collection of specified elements to the deque. | c - collection of elements to be added Return value - true |
void addFirst(Element e) | Inserts an element at the beginning of the deque | e - the element to be inserted |
void addLast(Element e) | Inserts an element at the end of the deque | e - the element to be inserted |
Boolean contains(Object o) | Checks if the deque contains the specified element | Return value - true if the deque contains the element |
int drainTo(Collection c) | Removes the elements from the deque and adds it to the specified collection | |
Object element() | Returns the first element(head) in the deque | |
Object getFirst() | Returns the first element(head) in the deque | |
Object getLast() | Returns the last element(tail) in the deque | |
Iterator iterator() | Retrieves the iterator of deque in sequence | Return value - Iterator |
Boolean offer(Object e) | Inserts the element as the tail | e - element to be added |
Boolean offerFirst(Object e) | Inserts the element at the front of the deque | e - element to be added |
Boolean offerLast(Object e) | Inserts the element at the end of the deque | e - element to be added |
Object peek() | Retrieves the first element of the deque(head) | Returns null if the deque is empty |
Object peekFirst() | Retrieves the first element of the deque(head) | Returns null if the deque is empty |
Object peekLast() | Retrieves the last element of the deque(tail) | Returns null if the deque is empty |
Object poll() | Retrieves and removes the first element of the deque(head) | Returns null if the deque is empty |
Object pollFirst() | Retrieves and removes the first element of the deque(head) | Returns null if the deque is empty |
Object pollLast() | Retrieves and removes the last element of the deque(tail) | Returns null if the deque is empty |
Object pop() | Retrieves or removes the first element from the stack of the deque | |
void push(Object e) | Inserts the element to the front of the deque | e - the element to be added |
void put(Object e) | Inserts the specified element into the deque | e - element to be inserted |
void putFirst(Object e) | Inserts the specified element at the beginning of the deque | e - element to be inserted |
void putLast(Object e) | Inserts the specified element at the end of the deque | e - element to be inserted |
Object remove() | Removes the first element from the deque | |
Boolean remove(Object o) | Removes the first occurrence of the specified object from the deque if present | o - The element that needs to be removed Return value - true if deque contains the element |
Object removeFirst() | Removes the first element of the deque | |
Boolean removeFirstOccurence(Object e) | Removes the first occurrence of the element specified in the deque | e - the element to be removed |
Object removeLast() | Removes the last element from the deque | |
Boolean removeLastOccurence(Object e) | Removes the last occurrence of the specified element from the deque | e - the element to be removed |
int size() | Fetches the size of the deque | Return value - size of the deque |
Element take() | Retrieves and removes the head element from the deque | Return value - the head element that is removed from the deque |
Element takeFirst() | Retrieves and removes the first element from the deque | Return value - the first element that is removed from the deque |
Element takeLast() | Removes the last element from the deque | Return value - the last element present in the deque |
Java BlockingDeque Example
Now let us see various examples of the BlockingDeque methods in the below section.
Example: Insert elements
The below example shows how to insert elements using the various methods in the BlockingDeque in Java. The addFirst()
, offerFirst()
, putFirst()
and push()
inserts the elements at the beginning of the deque.
import java.util.concurrent.BlockingDeque; import java.util.concurrent.LinkedBlockingDeque; public class BlockingDequeDemo { public static void main(String[] args) throws InterruptedException { BlockingDeque<String> bq = new LinkedBlockingDeque<String>(); System.out.println("Inserting elements..."); bq.add("Apple"); bq.add("Banana"); bq.addFirst("Watermelon"); bq.addLast("Pineapple"); System.out.println("Elements inserted using add operations: " + bq); bq.offer("Lemon"); bq.offerFirst("Orange"); bq.offerLast("Sapota"); System.out.println("Elements inserted using offer operations: " + bq); bq.push("Grapes"); System.out.println("Element inserted using push operation: " + bq); bq.put("Mosambi"); bq.putFirst("Guava"); bq.putLast("Papaya"); System.out.println("Elements inserted using put operations: " + bq); } }
Inserting elements... Elements inserted using add operations: [Watermelon, Apple, Banana, Pineapple] Elements inserted using offer operations: [Orange, Watermelon, Apple, Banana, Pineapple, Lemon, Sapota] Element inserted using push operation: [Grapes, Orange, Watermelon, Apple, Banana, Pineapple, Lemon, Sapota] Elements inserted using put operations: [Guava, Grapes, Orange, Watermelon, Apple, Banana, Pineapple, Lemon, Sapota, Mosambi, Papaya]
Example: Remove elements
The below example shows how to remove elements from the BlockingDeque using various methods. The remove()
, removeFirst()
, pollFirst()
and takeFirst()
methods retrieve and remove the first head element from the queue. The removeLast()
, pollLast()
, takeLast()
methods retrieve and remove the last element from the queue.
import java.util.concurrent.BlockingDeque; import java.util.concurrent.LinkedBlockingDeque; public class BlockingDequeDemo { public static void main(String[] args) throws InterruptedException { BlockingDeque<String> bq = new LinkedBlockingDeque<String>(); System.out.println("Inserting elements..."); bq.add("Apple"); bq.add("Banana"); bq.addFirst("Watermelon"); bq.addLast("Pineapple"); System.out.println("Elements inserted using add operations: " + bq); bq.offer("Lemon"); bq.offerFirst("Orange"); bq.offerLast("Sapota"); System.out.println("Elements inserted using offer operations: " + bq); bq.push("Grapes"); System.out.println("Element inserted using push operation: " + bq); bq.put("Mosambi"); bq.putFirst("Guava"); bq.putLast("Papaya"); System.out.println("Elements inserted using put operations: " + bq); System.out.println("\n Removing elements..."); bq.remove(); bq.remove("Lemon"); bq.removeFirst(); bq.removeLast(); System.out.println("Elements after remove operations: " + bq); System.out.println("Poll element: " + bq.poll()); System.out.println("PollFirst element: " + bq.pollFirst()); System.out.println("PollLast element: " + bq.pollLast()); System.out.println("Elements after poll operations: " + bq); System.out.println("Pop element: " + bq.pop()); System.out.println("Take element: " + bq.take()); System.out.println("TakeFirst element: " + bq.takeFirst()); System.out.println("TakeLast element: " + bq.takeLast()); System.out.println("Elements after take operations: " + bq); } }
Inserting elements... Elements inserted using add operations: [Watermelon, Apple, Banana, Pineapple] Elements inserted using offer operations: [Orange, Watermelon, Apple, Banana, Pineapple, Lemon, Sapota] Element inserted using push operation: [Grapes, Orange, Watermelon, Apple, Banana, Pineapple, Lemon, Sapota] Elements inserted using put operations: [Guava, Grapes, Orange, Watermelon, Apple, Banana, Pineapple, Lemon, Sapota, Mosambi, Papaya] Removing elements... Elements after remove operations: [Orange, Watermelon, Apple, Banana, Pineapple, Sapota, Mosambi] Poll element: Orange PollFirst element: Watermelon PollLast element: Mosambi Elements after poll operations: [Apple, Banana, Pineapple, Sapota] Pop element: Apple Take element: Banana TakeFirst element: Pineapple TakeLast element: Sapota Elements after take operations: []
Example: Access elements
The below example shows how to access the head and tail elements of the BlockingDeque using various methods. The element(), peekFirst()
and getFirst()
methods retrieve the head element. The peekLast()
and getLast()
methods retrieve the tail element.
import java.util.concurrent.BlockingDeque; import java.util.concurrent.LinkedBlockingDeque; public class BlockingDequeDemo { public static void main(String[] args) throws InterruptedException { BlockingDeque<String> bq = new LinkedBlockingDeque<String>(); System.out.println("Inserting elements..."); bq.add("Apple"); bq.add("Banana"); bq.addFirst("Watermelon"); bq.addLast("Pineapple"); System.out.println("Elements inserted using add operations: " + bq); bq.offer("Lemon"); bq.offerFirst("Orange"); bq.offerLast("Sapota"); System.out.println("Elements inserted using offer operations: " + bq); bq.push("Grapes"); System.out.println("Element inserted using push operation: " + bq); bq.put("Mosambi"); bq.putFirst("Guava"); bq.putLast("Papaya"); System.out.println("Elements inserted using put operations: " + bq); System.out.println("Element result: " + bq.element()); System.out.println("getFirst element: " + bq.getFirst()); System.out.println("getLast element: " + bq.getLast()); System.out.println("Peek element: " + bq.peek()); System.out.println("PeekFirst element: " + bq.peekFirst()); System.out.println("PeekLast element: " + bq.peekLast()); } }
Inserting elements... Elements inserted using add operations: [Watermelon, Apple, Banana, Pineapple] Elements inserted using offer operations: [Orange, Watermelon, Apple, Banana, Pineapple, Lemon, Sapota] Element inserted using push operation: [Grapes, Orange, Watermelon, Apple, Banana, Pineapple, Lemon, Sapota] Elements inserted using put operations: [Guava, Grapes, Orange, Watermelon, Apple, Banana, Pineapple, Lemon, Sapota, Mosambi, Papaya] Element result: Guava getFirst element: Guava getLast element: Papaya Peek element: Guava PeekFirst element: Guava PeekLast element: Papaya
Example: Iterate elements
Using the iterator()
method, we can traverse through every element in the BlockingDeque. For this we need to create an instance of the Iterator and then using the next()
method within a while loop, we can retrieve every element.
import java.util.Iterator; import java.util.concurrent.BlockingDeque; import java.util.concurrent.LinkedBlockingDeque; public class BlockingDequeDemo { public static void main(String[] args) throws InterruptedException { BlockingDeque<String> bq = new LinkedBlockingDeque<String>(); bq.add("Apple"); bq.add("Banana"); bq.addFirst("Watermelon"); bq.addLast("Pineapple"); bq.offer("Lemon"); bq.offerFirst("Orange"); bq.offerLast("Sapota"); bq.push("Grapes"); bq.put("Mosambi"); bq.putFirst("Guava"); bq.putLast("Papaya"); System.out.println("Iterating elements..."); Iterator<String> it = bq.iterator(); while(it.hasNext()) System.out.print(it.next() + " "); } }
Iterating elements... Guava Grapes Orange Watermelon Apple Banana Pineapple Lemon Sapota Mosambi Papaya