Home » Java Deque and ArrayDeque

Java Deque and ArrayDeque

by Online Tutorials Library

Java Deque Interface

Java Deque Interface is a linear collection that supports element insertion and removal at both ends. Deque is an acronym for “double ended queue”.


Deque Interface declaration

Methods of Java Deque Interface

Method Description
boolean add(object) It is used to insert the specified element into this deque and return true upon success.
boolean offer(object) It is used to insert the specified element into this deque.
Object remove() It is used to retrieves and removes the head of this deque.
Object poll() It is used to retrieves and removes the head of this deque, or returns null if this deque is empty.
Object element() It is used to retrieves, but does not remove, the head of this deque.
Object peek() It is used to retrieves, but does not remove, the head of this deque, or returns null if this deque is empty.

java arraydeque hierarchy

ArrayDeque class

The ArrayDeque class provides the facility of using deque and resizable-array. It inherits AbstractCollection class and implements the Deque interface.

The important points about ArrayDeque class are:

  • Unlike Queue, we can add or remove elements from both sides.
  • Null elements are not allowed in the ArrayDeque.
  • ArrayDeque is not thread safe, in the absence of external synchronization.
  • ArrayDeque has no capacity restrictions.
  • ArrayDeque is faster than LinkedList and Stack.

ArrayDeque Hierarchy

The hierarchy of ArrayDeque class is given in the figure displayed at the right side of the page.

ArrayDeque class declaration

Let’s see the declaration for java.util.ArrayDeque class.


Java ArrayDeque Example

Output:

Ravi Vijay Ajay 

Java ArrayDeque Example: offerFirst() and pollLast()

Output:

After offerFirst Traversal... jai arvind vimal mukul After pollLast() Traversal... jai arvind vimal 

Java ArrayDeque Example: Book

Output:

101 Let us C Yashwant Kanetkar BPB 8 102 Data Communications & Networking Forouzan Mc Graw Hill 4 103 Operating System Galvin Wiley 6 
Next TopicJava Map Interface

You may also like