Java

Deque Interface – Java Collections

Deque is a queue in which components from both sides of the queue may be added and removed. PriorityQueue is an example of how to delete and add pieces depending on priority. In the Java Queue lesson, we saw that the Queue follows FIFO (First in First out). We will demonstrate how to utilize Deque in this tutorial.

LinkedList and ArrayDeque are two implementations of the interface Deque. Because both classes, LinkedList and ArrayDeque, implement the Deque interface, we may construct instances of them and assign them to the Deque in the following ways:

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?

Deque is a queue that allows for the addition and deletion of parts from both sides. An example of how to remove and add parts according to priority is PriorityQueue. We learned that the Java Queue follows the FIFO principle in that course (First in First out). In this lesson, we’ll show you how to use the Deque library.

The interface Deque has two implementations: LinkedList and ArrayDeque. We may create instances of LinkedList and ArrayDeque and add them to the Deque in the following methods since both classes implement the Deque interface:

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(): This function returns the Deque’s first element.

E getLast() retrieves the Deque’s last element.

E peekFirst(): Returns null if the deque is empty or the first element in the deque.

E peekLast(): Returns the Deque’s last element, or null if it’s empty.

E pollFirst(): Returns and deletes the Deque’s first element, or null in the case of an empty Deque.

When using E pollLast(), the last element of the Deque is returned and removed, or null if the Deque is empty.

The number of elements in a Deque is returned by the int size() function.

Related Articles

Leave a Reply

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

Back to top button