Artikel Terbaru

Friday, 10 February 2012

Deque examples with tutorials in java

deque in java:-


Deque is abbrevateted 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 deque, we can inserts and deletes the objects from the both start, end of the the collection. where as normal collection inserts/deletes are happeneing 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 interface so you can not create a objects,Deque is implemented by two classes ArrayDequeand LinkedList.

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

So you got the basic idea on ArrayQueue, When i am learning Deque, 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 for these questions are found in my below table.

ArrayList ArrayDeque
Elemetns 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 are growable array of objects ArrayDeque are growable array of objects,better for implementing statck(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

Basic Deque examples :-

1.How to add elements to Queue?

For queue, we can add object to deque in may ways one way is using add version methods other is offer 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 list of objects
addFirst() method adds the elements starting list of objects
addLast() method adds the lements 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 fixed size of collection, we can use Offer version of methods.
if we add objects using Add methods for 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 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 version of methods also do the same thing like poll() methods except it throws NoSuchElementException if 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 delted element.

3.How to Access elements in Queue?:-
traversing elements in deque is same as 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 queue is empty, where as peek returns empty

4.How to find if element exists in deque?:-
dequelist.contains("stocks");

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


Please share your comments if you like this posts.