Friday, February 10, 2012

Deque examples with tutorials in java

Deque Object in java:-

Learn Java Deque example and tutorials
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.

ArrayList ArrayDeque
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

Deque interface Methods.

The following table contains basic methods that are used in our applications.
Methods Description
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 a various example of Dequa class in java.

1. 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 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

2. How to remove elements in Deque?:-

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
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.

3.How to Access elements in Queue?:-

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
(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 commondities stocks

get the specific elements in deque:-
dequelist.element()
dequelist.peek()
the above methods retrieves the elements from the queue, elemetns throws NoSuchElementException if the queue is empty, where as peek returns empty

4.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

Please share your comments if you like this posts.

3 comments


EmoticonEmoticon

Note: Only a member of this blog may post a comment.