java Deque object with examples

Deque Object in java

Deque is abbreviated as a double-ended queue, It is like a normal queue, which can store the collection of objects.

It is one of the collections classes introduced in Java 6. Why deque is different from other collection classes? the answer is, in the deque, we can insert and delete the objects from both starts, end of the collection. whereas normal collection inserts/deletes are happening at last only.

Deque is interface introduced in java 6. java.util.collections package which implements java.util package.

Deque interface extends java.util.Queue which in turns extends java.util.Collection, so we can insert/delete/iterate of all the objects that collections provides.

Deque is an interface so you can not create an object, Deque is implemented by two classes ArrayDequeand LinkedList.

ArrayQueue is just like array list implementation of the queue, LinkedList is the implementation of the double linked list.

So you got the basic idea on ArrayQueue, When I am learning Deque, the ArrayList class came into my mind, and immediately question came into my mind,

What is the difference between ArrayQueue and ArrayList?

When to use ArrayList and ArrayQueue?.

Answers to these questions are found in the below table.

ArrayListArrayDeque
Elements are inserted/deleted at any location including lastElements are inserted/deleted at both ends i.e start and last
Elements are accessed by indexelements are accessed by starting index or ending index
ArrayList is a growable array of objectsArrayDeque is a growable array of objects, better for implementing stack(LIFO) or Queue(FIFO)
Performances better for accessing elements, not for insert/delete operationsQueue performs better for insert/delete operations compared to access elements

Deque interface Methods.

The following table contains basic methods that use in our applications.

MethodsDescription
Boolean add(element)inserts or add the elements at the end of the queue
void addFirst(element)Add element at start of queue
void addLast(element)Add element at end of queue
boolean contains(element)returns if elements contains in queue
Iterator iterator()Returns iterator for queue
void addLast(element)Add element at end of queue
boolean offer(element)Add element at end of queue and returns true if inserted, else return false- no space
boolean offerFirst(element)Add element at start of queue and returns true if inserted, else return false- no space
boolean offerLast(element)Add element at end of queue and returns true if inserted, else return false- no space
element peek()return first element but not removed from start of deque, else null if it is empty
element poll()return and removes element from start of deque else null if it is empty
element pop()removes element from start of deque else null if it is empty
element push()add element from start of deque else null if it is empty

Basic Deque examples.

Let us go through various examples of Dequa class in java.

How to add elements to Queue?

For the queue, we can add an object to deque in many ways. one way is using add version methods, other is offering version methods as well as push() method

Deque dequelist=new ArrayDeque();
dequelist.add("equity");
dequelist.addFirst("Derivative");
dequelist.addLast("commondities");
or
dequelist.offer("equity");
dequelist.offerFirst("Derivative");
dequelist.offerLast("commondities");

(or)
dequelist.push("stocks");
System.out.println(" "+dequelist);

and output is

equity Derivative commodities stocks
  • add() method_ ads the elements at starting a list of objects
  • addFirst() method adds the elements starting a list of objects
  • addLast() method adds the elements end of the list.
  • offer() version of methods also do the same as like add() version of methods. but the difference is if the deque is a fixed size of the collection, we can use the Offer version of methods. if we add objects using Add methods for the fixed queue, it throws IllegalStateException if objects are unable to add the objects.

There is one more method i.e push() which adds an element to the end of the queue, returns true if added, otherwise returns false, throws IllegalStateException if not enough space in the collection

How to remove elements in Deque?

Deque API provides many types of methods to delete the objects from the queue

  • remove version methods
  • Poll Version methods
  • using pop() method
dequelist.remove("equity");
dequelist.removeFirst("Derivative");
dequelist.removeLast("commondities");
or
dequelist.poll("equity");
dequelist.pollFirst("Derivative");
dequelist.pollLast("commondities");
(or)
dequelist.pop("stocks")

deque provides the following methods to remove elements.

  • poll() method deletes the first matched object from the deque.
  • pollFirst() method deletes the first object from the deque.
  • pollLast() method deletes the last object from the deque.

remove the version of methods also do the same thing like poll() methods except it throws NoSuchElementException if the collection is empty. poll() methods returns null if the deque is empty.

there is one more method i.e pop() which deletes an element from the queue, returns deleted element.

How to iterate elements in Queue?

traversing elements in deque is the same as a list. We can use either Iterator, for each loop, or using specific methods using the peek() method

Iterate objects in Queue:-

//using iterator for deque
Iterator forwardIterator=dequelist.Iterator(); // for forward iteration
(or)
Iterator BackwardsIterator=dequelist.descendingIterator(); // for iteration of elements in reverse order
while(forwardIterator.next()){
System.out.println(" "+(String)forwardIterator.nextElement());
}
//using for each loop in deque
for(String element:dequelist){
System.out.print(element);
}

and output is

equity Derivative commodities stocks

get the specific elements in deque:-

dequelist.element()
dequelist.peek()

the above methods retrieve the elements from the queue, elements throw NoSuchElementException if the queue is empty, whereas peek returns empty

How to find if an element exists in deque?

dequelist.contains("stocks");

To check an element available in the queue, we have to use contains(Object) method, returns true if available, else return false

Conclusion

We learned Deque class in java with basic examples.