AppDividend
Latest Code Tutorials

Deque Interface In Java Example | Java Deque Interface

0

Deque Interface In Java Example | Java Deque Interface is today’s topic. The DEQUE is related to a data structure known as a queue. It is mainly known as the double-ended queue, which supports the addition or removal of data from either end. It is present in java.util package.

Deque Interface In Java

The Java Deque interface is the subtype of the Java Queue interface which means that you can use the Java Deque like the Queue if you should ever have the need.

Deque Implementations

Since Java Deque is the interface, you need to instantiate a concrete implementation of an interface to use it.

You can choose Deque implementations in the Java Collections API:

  1. java.util.LinkedList
  2. java.util.ArrayDeque

The LinkedList class is the standard Deque and Queue implementation.

It uses the linked list internally to model the queue or a deque.

The Java ArrayDeque class stores its items internally in the array.

If the number of elements exceeds the space in an array, the new array is allocated, and all items moved over.

In other words, an ArrayDeque grows as needed, even if it stores its items in an array.

Deque Interface Declaration

public interface Deque<E> extends Queue<E> 

Features of Deque

  1. We can add and remove data from both sides of the queue.
  2. Concurrent access is not supported using multiple threads.
  3. Deque is not thread-safe in the absence of external synchronization.
  4. It does not have any capacity restrictions.
  5. Array Deque does not accept NULL elements.
  6. Array Deque is faster than the stack and linked list.

Methods of Deque

  1. add(element): It is used for adding an item to the tail of the deque.
  2. addFirst(element): It is used for adding an element to the head of the deque.
  3. addLast(element): It is used for adding an element to the tail of the deque.
  4. offer(element): It is used for adding an element to the rear and returns a boolean, i.e. True or False if the insertion was successful.
  5. offerFirst(element): It is used for adding an element to the head and returns a boolean, i.e. True or False if the insertion was successful.
  6. offerLast(element): It is used for adding an element to the tail and returns a boolean, i.e. True or False if the insertion was successful.
  7. iterator(): It returns an iterator for the present deque.
  8. descendingIterator(): It returns an iterator that follows the reverse order of the corresponding deque.
  9. push(element): It is used for adding an element to the head.
  10.  pop(element): It is used for removing an item from the head and later returns it.
  11.  removeFirst(): It is used for removing the element at the head.
  12.  removeLast(): It is used for removing the item at the tail.
  13.  poll(): It retrieves and removes the first item of the deque and returns null if that deque is empty.
  14.  pollFirst(): It retrieves and removes the first item of the deque and returns null if that deque is empty.
  15.  pollLast(): It retrieves and removes the last item of the deque and returns null if that deque is empty.
  16.  peek(): It retrieves, but does not delete the first item of this deque or returns null if the deque is empty.
  17.  peekFirst(): It retrieves, but does not delete the first item of this deque or returns null if the deque is empty.
  18. peekLast(): It retrieves, but does not delete the last item of this deque or returns null if the deque is empty.

Let’s look at an example to clear Deque Interface in Java.

import java.util.*;

public class DequeExample {
  public static void main(String[] args) {
    Deque<String> deque = new LinkedList<String>();
    deque.add("Element 1 (Tail)");
    deque.addFirst("Element 2 (Head)");
    deque.addLast("Element 3 (Tail)");
    deque.push("Element 4 (Head)");
    deque.offer("Element 5 (Tail)");
    deque.offerFirst("Element 6 (Head)");
    deque.offerLast("Element 7 (Tail)");
    System.out.println(deque + "\n");

    // Making an iterator to traverse queue
    System.out.println("Standard Iterator");
    Iterator iterator = deque.iterator();
    while (iterator.hasNext())
      System.out.println("\t" + iterator.next());

    // Reverse order iterator
    Iterator reverse = deque.descendingIterator();
    System.out.println("Reverse Iterator");
    while (reverse.hasNext())
      System.out.println("\t" + reverse.next());

    System.out.println("Peek " + deque.peek());
    System.out.println("After peek: " + deque);

    System.out.println("Pop " + deque.pop());
    System.out.println("After pop: " + deque);

    System.out.println("Contains element 3: " + deque.contains("Element 3 (Tail)"));
    deque.removeFirst();
    deque.removeLast();
    System.out.println("Deque after removing " + "first and last: " + deque);

  }
}

See the output.

 

Deque Interface In Java

EXPLANATION

In the above code, we discussed the essential functions and how they work in Java.

See the second following code example.

import java.util.*;

class Book {
  int id, qty;
  String name, author;

  public Book(int id, String name, String author, int qty) {
    this.id = id;
    this.name = name;
    this.author = author;
    this.qty = qty;
  }
}

public class ArrayDequeExample {
  public static void main(String[] args) {
    Deque<Book> set = new ArrayDeque<Book>();
    Book b1 = new Book(101, "Java:The Complete Reference", "Herbert Schildt", 8);
    Book b2 = new Book(102, "Data Communications & Networking", "Forouzan", 4);
    Book b3 = new Book(103, "Operating System", "Galvin", 6);
    set.add(b1);
    set.add(b2);
    set.add(b3);
    for (Book b : set) {
      System.out.println(b.id + " " + b.name + " " + b.author + " " + b.quantity);
    }
  }
}

See the output.

 

Java Deque Interface

EXPLANATION

In the above code, we created Array Deque which stores the id, name, author, and quantity of the books, and later we displayed all the information of the books.

Iterate Elements of Deque

You can also iterate all items of the Java Deque, instead of just processing the items one at a time.

You can iterate the items of a Deque in two ways:

  1. Using an Iterator.
  2. Using the for-each loop.

A sequence in which the items are obtained during iteration depends on the concrete Deque implementation.

However, the method to iterate the items is the same regardless of application.

Iterate Elements via Iterator

The first way of iterating the items of the Deque is to obtain the Iterator from the Deque and iterate the items via that.

Here is an example of iterating the items of the Java Deque via an Iterator.

Deque deque = new LinkedList();

deque.add("element 0");
deque.add("element 1");
deque.add("element 2");

Iterator iterator = deque.iterator();
while(iterator.hasNext(){
  String element = (String) iterator.next();
}

Iterate Elements via For-Each Loop

The second way to iterate the items of the Deque is to use the for-each loop in Java.

Here is the example of iterating the items of a Java Deque via the for-each loop.

Deque deque = new LinkedList();

deque.add("element 0");
deque.add("element 1");
deque.add("element 2");

for(Object object : deque) {
    String element = (String) object;
}

Finally, Deque Interface In Java Example | Java Deque Interface is over.

Recommended Posts

Java Literals Example

Java PriorityQueue Example

Java Thread Pools Tutorial

Set Example In Java Tutorial

Dictionary Class In Java Tutorial

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.