Home Tutorial Java Core Collection : Deque Example

 
 

Share on Google+Share on Google+
Collection : Deque Example
Posted on: November 9, 2012 at 12:00 AM
Advertisement
In this tutorial we will describe implementation of Queue with example.

Collection : Deque Example

In this tutorial we will describe implementation of Queue with example.

Deque :

A java.util.Deque interface is linear collection which allows insertion and removal at both ends. It stands for "double ended queue".
Deque defines many methods for insertion, deletion and access elements at the both ends of the deque. Here we defining some of them -

offer(Object o) :  It is of Boolean type and works like add(o) method of Collection and inserts the specified element into your deque.
It returns true if addition of element is possible else returns false.

offerFirst(Object o) : It is of Boolean type, inserts element at the front of deque if possible otherwise return false.

offerLast(Object o) : Boolean type. It inserts element at the end of deque if insertion possible otherwise it returns false.

Other element insertion methods are -add(Object o), addFirst(Object o), addLast(Object o)

poll() : It retrieves and removes the head of deque. It returns null if deque is empty.

pollLast() : This method retrieves and removes the last element of your deque. It returns null if deque is empty.

pollFirst() : This method retrieves and removes the first element of your deque. It returns null if deque is empty.

There are many others method to handle different operations of deque.

Example :

package collection;

import java.util.Deque;
import java.util.LinkedList;

class DequeExample{
public static void main(String[] args){
Deque deque=new LinkedList();

//Adding element to the Deque
deque.add(44);
deque.offer("test");
deque.offerFirst("Hello");
deque.addLast("Roseindia");
deque.offer(11);

System.out.println("size of deque : "+deque.size());
System.out.println("Elements in deque: "+deque);

//Deleting element from deque
deque.remove();
deque.pollFirst();
System.out.println("size of deque : "+deque.size());
System.out.println("Elements in deque: "+deque);

System.out.println("Last element in deque: "+deque.peekLast());
System.out.println("Top element of deque : "+deque.element());

}
}

Description : This example implements Deque interface. You can create object of deque as - Deque deque=new LinkedList();
There are various methods to add element to the deque. By using offerFirst() or addFirst method we can add element at the first position of deque.
Next by using size() methods we are printing size of deque and then content of deque. For deleting element from the deque you can call poll() method or remove() method. In our program we are calling pollFirst() method to remove first element of deque. By using peek() or element method we can display head element of deque.

Output :

size of deque : 5
Elements in deque: [Hello, 44, test, Roseindia, 11]
size of deque : 3
Elements in deque: [test, Roseindia, 11]
Last element in deque: 11
Top element of deque : test
Advertisement

Related Tags for Collection : Deque Example:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 9, 2012

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com