BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Deque Interface in Java Collections

By Chaitanya Singh | Filed Under: java

Deque is a Queue in which you can add and remove elements from both sides. In the Java Queue tutorial we have seen that the Queue follows FIFO (First in First out) and in PriorityQueue example we have seen how to remove and add elements based on the priority. In this tutorial, we will see how to use Deque.

Deque is an interface and has two implementations: LinkedList and ArrayDeque. By implementation I refer that these classes LinkedList and ArrayDeque implements Deque interface, so we can create the instance of these and assign it to the Deque like this:

Deque dq = new LinkedList();
Deque dq = new ArrayDeque();

Java Deque Interface Example

import java.util.Deque;
import java.util.ArrayDeque;

public class ArrayDequeExample {

   public static void main(String[] args) {
		  
       /*
	* We cannot create instance of a Deque as it is an
	* interface, we can create instance of ArrayDeque or
	* LinkedList and assign it to Deque
	*/
       Deque<String> dq = new ArrayDeque<String>();
	    
       /*
	* Adding elements to the Deque.
	* addFirst() adds element at the beginning 
        * and addLast() method adds at the end.
	*/
	dq.add("Glenn");
	dq.add("Negan");
	dq.addLast("Maggie");
	dq.addFirst("Rick");
	dq.add("Daryl");
	    
	System.out.println("Elements in Deque:"+dq);

        /*
	 * We can remove element from Deque using remove() method,
	 * we can use normal remove() method which removes first 
	 * element or we can use removeFirst() and removeLast()
	 * methods to remove first and last element respectively.
	 */
	System.out.println("Removed element: "+dq.removeLast());
	    
	/*
	 * element() method - returns the head of the
	 * Deque. Head is the first element of Deque
	 */
	 System.out.println("Head: "+dq.element());
	    
	/*
	 * pollLast() method - this method removes and returns the 
	 * tail of the Deque(last element). Returns null if the Deque is empty.
	 * We can also use poll() or pollFirst() to remove the first element of
	 * Deque.
	 */
	System.out.println("poll(): "+dq.pollLast());
	    
	/*
	 * peek() method - it works same as element() method,
	 * however it returns null if the Deque is empty. We can also use 
	 * peekFirst() and peekLast() to retrieve first and last element
	 */
	System.out.println("peek(): "+dq.peek());
	    
	//Again printing the elements of Deque
	System.out.println("Elements in Deque:"+dq);
   }
}

Output:

Elements in Deque:[Rick, Glenn, Negan, Maggie, Daryl]
Removed element: Daryl
Head: Rick
poll(): Maggie
peek(): Rick
Elements in Deque:[Rick, Glenn, Negan]

When to use ArrayList and when to use ArrayDeque?

ArrayDeque has the ability to add or remove the elements from both ends (head or tail), on the other hand removing last element from ArrayList takes O(n) time as it traverses the whole list to reach the end. So if you want to add or remove elements from both ends choose ArrayDeque over ArrayList, however if you only want to perform the opreation on the tail (at the end) then you should choose ArrayList.

Methods of Deque interface

void addFirst(E e): Inserts the specified element at the beginning of the Deque.

void addLast(E e): Inserts the specified element at the end of the Deque.

boolean contains(Object o): Returns true if the specified element is present in the Deque.

E getFirst(): It returns the first element of the Deque.

E getLast(): It returns the last element of the Deque.

E peekFirst(): Returns the first element of Deque, or null if the Deque is empty.

E peekLast(): Returns the last element of Deque, or null if the Deque is empty.

E pollFirst(): Returns and removes the first element of Deque, or null if the Deque is empty.

E pollLast(): Returns and removes the last element of Deque, or null if the Deque is empty.

int size(): Returns the number of elements present in Deque.
Deque Official Documentation

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap