Java Deque Interface

  • Deque is one of the collection interface which extends Queue interface.
  • Deque is linear collection which allow us to add and remove element from both ends of the queue.
  • It is "double ended queue" that's why it is called Deque and usually pronounced as "deck".
  • Deque has different methods to perform insert, delete and examine the element in queue.
  • Each of these methods have two different forms. One will throw an exception if it fails during operation and other will return a special value like null or false.
java deque interface

Different methods of Deque interface are described below.

  • boolean add(E e) : It will insert specified element at the tail of this deque if space is currently available. Return true if operation is success or throw an IllegalStateException if fails to perform operation.
  • void addFirst(E e) : It will insert specified element at the beginning of the queue.
  • void addLast(E e) : It will insert specified element at the end of the queue.
  • boolean contains(Object o) : It will return true if specified element is available in queue.
  • E element() : It will retrieve head(first) element from queue.
  • E getFirst() : It will retrieve first element from queue.
  • E getLast() : It will retrieve last element from queue.
  • boolean offer(E e) : It will insert specified element at the tail of this deque. It will return true if success and false if operation fails.
  • boolean offerFirst(E e) : It will insert specified element at the beginning of queue if space is available.
  • boolean offerLast(E e) : It will insert specified element at the end of queue if space is available.
  • E peek() : It will retrieve head(first) element from queue. Returns null if queue is empty.
  • E peekFirst() : It will retrieve first element from queue. Returns null if queue is empty.
  • E peekLast() : It will retrieve last element from queue. Returns null if queue is empty.
  • E poll() : It will remove head(first) element from queue. Returns null if queue is empty.
  • E pollFirst() : It will remove first element from queue. Returns null if queue is empty.
  • E pollLast() : It will remove last element from queue. Returns null if queue is empty.
  • E pop() : It will pops the element from queue.
  • void push(E e) : It will pushes the element onto queue. It will return true on success and throe an exception if operation fails.
  • E remove() : It will remove first element from queue.
  • boolean remove(Object o) : It will remove first occurrence of the given element from queue.
  • E removeFirst() : It will remove first element from queue.
  • boolean removeFirstOccurrence(Object o) : It will remove first occurrence of the given element from queue.
  • E removeLast() : It will remove last element from queue.
  • boolean removeLastOccurrence(Object o) : It will remove last occurrence of the given element from queue.
  • int size() : It will return size of queue.
I have prepared example of ArrayDeque to explore you how different methods of Deque works. Also you can create LinkedList() using Deque.

Deque Example

package JAVAExamples;

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

public class DequeExample {

 public static void main(String[] args) {
  //Create ArrayDeque.
  Deque<String> d = new ArrayDeque<String>();
  //Add elements in Deque.
  d.add("one");
  d.add("two");
  d.add("three");
  d.add("four");
  d.add("five");
  d.add("four");
  d.add("six");
  //Print Deque elements.
  System.out.println("Deque elements are : "+d);
  
  //Add element at first of Deque.
  d.addFirst("First Added");
  System.out.println("Deque elements after addFirst are : "+d);
  
  //Add element at last of Deque.
  d.addLast("Last Added");
  System.out.println("Deque elements after addLast are : "+d);
  
  //Get first element from Deque.
  System.out.println("First element in deque is : "+d.getFirst());
  
  //Get last element from Deque.
  System.out.println("Last element in deque is : "+d.getLast());
  
  //Get first element from Deque using peek.
  System.out.println("First element in deque using peek is : "+d.peek());
  
  //Get last element from Deque using peekLast.
  System.out.println("Last element in deque using peekLast is : "+d.peekLast());
  
  //Remove first element from deque.
  d.removeFirst();
  System.out.println("Deque elements after removeFirst are  : "+d);
  
  //Using pop.
  d.pop();
  System.out.println("Deque elements after pop are  : "+d);
  
  //Using push.
  d.push("First");
  System.out.println("Deque elements after push are  : "+d);
  
  //Remove LastOccurrence of element from deque using removeLastOccurrence.
  d.removeLastOccurrence("four");
  System.out.println("Deque elements after removeLastOccurrence are  : "+d);
  
  //Get size of deque.
  System.out.println("Size of Deque is  : "+d.size());
 }
}

Output :
Deque elements are : [one, two, three, four, five, four, six]
Deque elements after addFirst are : [First Added, one, two, three, four, five, four, six]
Deque elements after addLast are : [First Added, one, two, three, four, five, four, six, Last Added]
First element in deque is : First Added
Last element in deque is : Last Added
First element in deque using peek is : First Added
Last element in deque using peekLast is : Last Added
Deque elements after removeFirst are  : [one, two, three, four, five, four, six, Last Added]
Deque elements after pop are  : [two, three, four, five, four, six, Last Added]
Deque elements after push are  : [First, two, three, four, five, four, six, Last Added]
Deque elements after removeLastOccurrence are  : [First, two, three, four, five, six, Last Added]
Size of Deque is  : 7

No comments:

Post a Comment