Open In App

Difference Between ArrayBlockingQueue and ArrayDeque

Last Updated : 20 Nov, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

ArrayBlockingQueue is a class in Java that implements the BlockingQueue interface. ArrayBlockingQueue class and its iterator implement all the optional methods of the Collection and Iterator interfaces. ArrayBlockingQueue is a bounded BlockingQueue backed by an array. Here, bounded means the size of the Queue is finite and fixed. Once created cannot grow or shrink the size of the Queue. If trying to insert an element into a full Queue then it will result in the operation blocking. Similarly, if trying to take an element from an empty Queue, then also the operation will be blocked. ArrayBlockingQueue stores the elements in the Queue internally in the FIFO (first-in-first-out) order. The element at the head or front of the Queue is the oldest element of all the elements present in this queue. The element at the tail of this queue is the newest element of all the elements of this queue. The new elements are always inserted at the end or tail of the queue and the retrieval operations obtain elements at the head of the queue.

ArrayBlockingQueue Implementation:

Java




// Java program to demonstrate ArrayBlockingQueue
  
import java.util.concurrent.ArrayBlockingQueue;
  
public class ArrayBlockingQueueDemo {
  
    public static void main(String[] args)
    {
        // define capacity of ArrayBlockingQueue
        int capacity = 15;
  
        // create object of ArrayBlockingQueue
        // using ArrayBlockingQueue constructor
        ArrayBlockingQueue<Integer> abq
            = new ArrayBlockingQueue<Integer>(capacity);
  
        // add numbers
        abq.add(1);
        abq.add(2);
        abq.add(3);
  
        // print queue
        System.out.println("ArrayBlockingQueue:" + abq);
    }
}


Output

ArrayBlockingQueue:[1, 2, 3]

ArrayDeque is a class in Java that implements both Queue and Deque. It is dynamically re-sizable from both sides. This is a special kind of array that grows and allows users to add or remove an element from both sides of the queue. It is also known as Array Double Ended Queue or Array Deck.

Few important features of ArrayDeque are as follows:  

  • ArrayDeque has no capacity restrictions, and they grow as necessary to support usage.
  • They are not thread-safe which means that in the absence of external synchronization, ArrayDeque does not support concurrent access by multiple threads.
  • Null elements are prohibited in the ArrayDeque.
  • ArrayDeque class is likely to be faster than Stack when used as a stack.
  • ArrayDeque class is likely to be faster than LinkedList when used as a queue.

ArrayDeque Implementation:

Java




// Java program to demonstrate ArrayDeque
  
import java.util.*;
  
public class ArrayDequeDemo {
  
    public static void main(String[] args)
    {
  
        // Initializing an deque
        Deque<Integer> de_que = new ArrayDeque<Integer>(10);
  
        // add numbers
        de_que.add(10);
        de_que.add(20);
        de_que.add(30);
  
        // print queue
        System.out.println("ArrayDeque:" + de_que);
    }
}


Output

ArrayDeque:[10, 20, 30]

Difference between ArrayBlockingQueue and ArrayDeque:

S.NO.                           ArrayBlockingQueue                  ArrayDeque
1.

ArrayBlockingQueue implements the BlockingQueue interface.

ArrayDeque implements the Deque interface.

2.

It is a fixed size array queue. Therefore, we cannot grow and shrink the size of an array once we created.

It is a resizable array queue. Therefore, we can grow and shrink the size of the array.

3.

Able to add or remove the elements from only one side of the queue.

Able to add or remove the elements from both sides of the queue.

4. 

ArrayBlockingQueue is thread-safe.

ArrayDeque is not thread-safe.

5.

ArrayBlockingQueue class is not faster than the ArrayDeque class.

ArrayDeque class is faster than the ArrayBlockingQueue class.



Similar Reads

Difference Between ArrayBlockingQueue and LinkedBlockingQueue
ArrayBlockingQueue and LinkedBlockingQueue in Java Collection are the common implementations of the BlockingQueue interface. ArrayBlockingQueue: ArrayBlockingQueue is a class in Java that implements the BlockingQueue interface. ArrayBlockingQueue class and its iterator implement all the optional methods of the Collection and Iterator interfaces. Ar
4 min read
ArrayBlockingQueue add() method in Java
ArrayBlockingQueue is bounded, blocking queue that stores the elements internally backed by an array. ArrayBlockingQueue class is a member of the Java Collections Framework. Bounded means it will have a fixed size, you can not store number the elements more than the capacity of the queue. The queue also follows FIFO (first-in-first-out) rule for st
3 min read
ArrayBlockingQueue toString() Method in Java
The toString() method of ArrayBlockingQueue class is used to get a string representation of the objects of ArrayBlockingQueue. The string of ArrayBlockingQueue contains elements of ArrayBlockingQueue in the order from first(head) to last(tail), enclosed in square brackets("[]"). The elements are separated by the characters", " (comma and a space).
2 min read
ArrayBlockingQueue size() Method in Java
ArrayBlockingQueue is bounded, blocking queue that stores the elements internally backed by an array. ArrayBlockingQueue class is a member of the Java Collections Framework.Bounded means it will have a fixed size, you can not store number the elements more than the capacity of the queue.The queue also follows FIFO (first-in-first-out) rule for stor
2 min read
ArrayBlockingQueue poll() Method in Java
ArrayBlockingQueue is bounded, blocking queue that stores the elements internally backed by an array. ArrayBlockingQueue class is a member of the Java Collections Framework. Bounded means it will have a fixed size, you can not store number the elements more than the capacity of the queue. The queue also follows FIFO (first-in-first-out) rule for st
4 min read
ArrayBlockingQueue peek() Method in Java
ArrayBlockingQueue is bounded, blocking queue that stores the elements internally backed by an array. ArrayBlockingQueue class is a member of the Java Collections Framework.Bounded means it will have a fixed size, you can not store number the elements more than the capacity of the queue.The queue also follows FIFO (first-in-first-out) rule for stor
3 min read
ArrayBlockingQueue put() method in Java
ArrayBlockingQueue is bounded, blocking queue that stores the elements internally backed by an array. ArrayBlockingQueue class is a member of the Java Collections Framework. Bounded means it will have a fixed size, you can not store number the elements more than the capacity of the queue. The queue also follows FIFO (first-in-first-out) rule for st
2 min read
ArrayBlockingQueue remove() method in Java
ArrayBlockingQueue is bounded, blocking queue that stores the elements internally backed by an array. ArrayBlockingQueue class is a member of the Java Collections Framework. Bounded means it will have a fixed size, you can not store number the elements more than the capacity of the queue. The queue also follows FIFO (first-in-first-out) rule for st
3 min read
ArrayBlockingQueue contains() method in Java
ArrayBlockingQueue is bounded, blocking queue that stores the elements internally backed by an array. ArrayBlockingQueue class is a member of the Java Collections Framework. Bounded means it will have a fixed size, you can not store number the elements more than the capacity of the queue. The queue also follows FIFO (first-in-first-out) rule for st
3 min read
ArrayBlockingQueue clear() Method in Java
ArrayBlockingQueue is bounded, blocking queue that stores the elements internally backed by an array. ArrayBlockingQueue class is a member of the Java Collections Framework.Bounded means it will have a fixed size, you can not store number the elements more than the capacity of the queue.The queue also follows FIFO (first-in-first-out) rule for stor
2 min read