Java - Deque example

Posted on October 27, 2016


Deque is a leaner collection of elements which support insertion and deletion of elements at both ends. It is pronounced as 'Double ended Queue'. The Deque interface provides the methods to insert, remove and examine the elements at both end of queue. These methods are

  • addFirst(), offerFirst() and push() - Insert elements to the head of the deque.
  • add(), addLast(), offerLast() and offer() - Insert elements to the tail of the deque.
  • element() ,getFirst(), peek() and peekFirst() - Retrieve elements from the head of the deque.
  • getLast() and peekLast() - Retrieve elements from the tail of the deque.
  • remove(), removeFirst(), poll(), pollFirst() and pop() - Remove elements from the head of deque.
  • removeLast() and pollLast() - Remove elements from the tail of the deque.

ArrayDeque and LinkedList are implementation of the Deque interface in Collection API.

DequeExample.java

package com.boraji.tutorial;

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

/**
 * @author imssbora
 *
 */
public class DequeExample {
	public static void main(String[] args) {
		Deque<String> deque=new LinkedList<>();
		/*Adding element to deque*/
		deque.add("One");
		deque.addFirst("Two");
		deque.addLast("Three");
		deque.offer("Four");
		deque.offerFirst("Five");
		deque.offerLast("Six");
		deque.push("Seven");
		
		
		System.out.println("Elements in deque: "+deque);
		System.out.println();
		
		/*Retrieving elements from deque*/
		System.out.println("element() : "+deque.element());
		System.out.println("getFirst() : "+deque.getFirst());
		System.out.println("getLast() : "+deque.getLast());
		System.out.println("peek() : "+deque.peek());
		System.out.println("peekFirst() : "+deque.peekFirst());
		System.out.println("peekLast() : "+deque.peekLast());
		
		/*Removing elements from deque */
		System.out.println();
		
		System.out.println("remove() : "+deque.remove());
		System.out.println("Elements in deque after removal : "+deque);
		
		System.out.println("removeFirst() : "+deque.removeFirst());
		System.out.println("Elements in deque after removal : "+deque);
		
		System.out.println("removeLast() : "+deque.removeLast());
		System.out.println("Elements in deque after removal : "+deque);
		
		System.out.println("poll() : "+deque.poll());
		System.out.println("Elements in deque after removal : "+deque);
		
		System.out.println("pollFirst() : "+deque.pollFirst());
		System.out.println("Elements in deque after removal : "+deque);
		
		System.out.println("pollLast() : "+deque.pollLast());
		System.out.println("Elements in deque after removal : "+deque);
		
		System.out.println("pop() : "+deque.pop());
		System.out.println("Elements in deque after removal : "+deque);
	}
}

Output

Elements in deque: [Seven, Five, Two, One, Three, Four, Six]

element() : Seven
getFirst() : Seven
getLast() : Six
peek() : Seven
peekFirst() : Seven
peekLast() : Six

remove() : Seven
Elements in deque after removal : [Five, Two, One, Three, Four, Six]
removeFirst() : Five
Elements in deque after removal : [Two, One, Three, Four, Six]
removeLast() : Six
Elements in deque after removal : [Two, One, Three, Four]
poll() : Two
Elements in deque after removal : [One, Three, Four]
pollFirst() : One
Elements in deque after removal : [Three, Four]
pollLast() : Four
Elements in deque after removal : [Three]
pop() : Three
Elements in deque after removal : []