Sunday, September 15, 2019

Java Deque Interface

Java Deque Interface is a linear collection that supports element insertion and removal at both ends. Deque is an acronym for "double ended queue".

Deque Interface declaration

  1. public interface Deque<E> extends Queue<E>  

Methods of Java Deque Interface

MethodDescription
boolean add(object)It is used to insert the specified element into this deque and return true upon success.
boolean offer(object)It is used to insert the specified element into this deque.
Object remove()It is used to retrieves and removes the head of this deque.
Object poll()It is used to retrieves and removes the head of this deque, or returns null if this deque is empty.
Object element()It is used to retrieves, but does not remove, the head of this deque.
Object peek()It is used to retrieves, but does not remove, the head of this deque, or returns null if this deque is empty.

java arraydeque hierarchy

ArrayDeque class

The ArrayDeque class provides the facility of using deque and resizable-array. It inherits AbstractCollection class and implements the Deque interface.
The important points about ArrayDeque class are:
  • Unlike Queue, we can add or remove elements from both sides.
  • Null elements are not allowed in the ArrayDeque.
  • ArrayDeque is not thread safe, in the absence of external synchronization.
  • ArrayDeque has no capacity restrictions.
  • ArrayDeque is faster than LinkedList and Stack.

ArrayDeque Hierarchy

The hierarchy of ArrayDeque class is given in the figure displayed at the right side of the page.

ArrayDeque class declaration

Let's see the declaration for java.util.ArrayDeque class.
  1. public class ArrayDeque<E> extends AbstractCollection<E> implements Deque<E>, Cloneable, Serializable  

Java ArrayDeque Example

  1. import java.util.*;  
  2. public class ArrayDequeExample {  
  3.    public static void main(String[] args) {  
  4.    //Creating Deque and adding elements  
  5.    Deque<String> deque = new ArrayDeque<String>();  
  6.    deque.add("Ravi");    
  7.    deque.add("Vijay");     
  8.    deque.add("Ajay");    
  9.    //Traversing elements  
  10.    for (String str : deque) {  
  11.    System.out.println(str);  
  12.    }  
  13.    }  
  14. }  
Output:
Ravi
Vijay
Ajay

Java ArrayDeque Example: offerFirst() and pollLast()

  1. import java.util.*;  
  2. public class DequeExample {  
  3. public static void main(String[] args) {  
  4.     Deque<String> deque=new ArrayDeque<String>();  
  5.     deque.offer("arvind");  
  6.     deque.offer("vimal");  
  7.     deque.add("mukul");  
  8.     deque.offerFirst("jai");  
  9.     System.out.println("After offerFirst Traversal...");  
  10.     for(String s:deque){  
  11.         System.out.println(s);  
  12.     }  
  13.     //deque.poll();  
  14.     //deque.pollFirst();//it is same as poll()  
  15.     deque.pollLast();  
  16.     System.out.println("After pollLast() Traversal...");  
  17.     for(String s:deque){  
  18.         System.out.println(s);  
  19.     }  
  20. }  
  21. }  
Output:
After offerFirst Traversal...
jai
arvind
vimal
mukul
After pollLast() Traversal...
jai
arvind
vimal

Java ArrayDeque Example: Book

  1. import java.util.*;    
  2. class Book {    
  3. int id;    
  4. String name,author,publisher;    
  5. int quantity;    
  6. public Book(int id, String name, String author, String publisher, int quantity) {    
  7.     this.id = id;    
  8.     this.name = name;    
  9.     this.author = author;    
  10.     this.publisher = publisher;    
  11.     this.quantity = quantity;    
  12. }    
  13. }    
  14. public class ArrayDequeExample {    
  15. public static void main(String[] args) {    
  16.     Deque<Book> set=new ArrayDeque<Book>();    
  17.     //Creating Books    
  18.     Book b1=new Book(101,"Let us C","Yashwant Kanetkar","BPB",8);    
  19.     Book b2=new Book(102,"Data Communications & Networking","Forouzan","Mc Graw Hill",4);    
  20.     Book b3=new Book(103,"Operating System","Galvin","Wiley",6);    
  21.     //Adding Books to Deque   
  22.     set.add(b1);    
  23.     set.add(b2);    
  24.     set.add(b3);    
  25.     //Traversing ArrayDeque  
  26.     for(Book b:set){    
  27.     System.out.println(b.id+" "+b.name+" "+b.author+" "+b.publisher+" "+b.quantity);    
  28.     }    
  29. }    
  30. }    
Output:
101 Let us C Yashwant Kanetkar BPB 8
102 Data Communications & Networking Forouzan Mc Graw Hill 4
103 Operating System Galvin Wiley 6

No comments:

Post a Comment

How to DROP SEQUENCE in Oracle?

  Oracle  DROP SEQUENCE   overview The  DROP SEQUENCE  the statement allows you to remove a sequence from the database. Here is the basic sy...