Deque is abbreviated as double-ended queue like a normal queue, can store the collection of objects. It is one of the collections class introduced in Java 6. Why deque is different from other collection classes? the answer is, in the deque, we can insert and deletes 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 my below table.
Elements are inserted/deleted at any location including last
Elements are inserted/deleted at both ends i.e start and last
Elements are accessed by index
elements are accessed by starting index or ending index
ArrayList is a growable array of objects
ArrayDeque are a growable array of objects, better for implementing stack(LIFO) or Queue(FIFO)
Performances better for accessing elements, not for insert/delete operations
Queue performs better for insert/delete operations compared to access elements
The following table contains basic methods that are used in our applications.
inserts or add the elements at the end of the queue
Add element at start of queue
Add element at end of queue
returns if elements contains in queue
Returns iterator for queue
Add element at end of queue and returns true if inserted, else return false- no space
Add element at start of queue and returns true if inserted, else return false- no space
return first element but not removed from start of deque, else null if it is empty
return and removes element from start of deque else null if it is empty
removes element from start of deque else null if it is empty
add element from start of deque else null if it is empty
Let us go through a various example of Dequa class in java.
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();
and output is
equity Derivative commondities 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 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 no enough space in the collection
Deque API provides many types of methods to delete the objects from the queue 1. remove version methods 2. Poll Version methods 3. using pop() method
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.
traversing elements in deque is same as a list, we can use either Iterator, for each loop, or using specific methods using peek()
Iterate objects in Queue:-
//using iterator for deque
Iterator forwardIterator=dequelist.Iterator(); // for forward iteration
Iterator BackwardsIterator=dequelist.descendingIterator(); // for iteration of elements in reverse order
//using for each loop in deque
get the specific elements in deque:-
the above methods retrieves the elements from the queue, elemetns throws NoSuchElementException if the queue is empty, where as peek returns empty
To check an element available in the queue, we have to use contains(Object) method, returns true if available, else return false
Anonymous - Apr 6, 2012
good article. greetings from chile
nice one…valueable to read…thank you