Thursday, February 25, 2016

Java Deque Interface

| Thursday, February 25, 2016
  • Deque is 1 of the collection interface which extends Queue interface.
  • Deque is linear collection which allow us to add together together with take chemical cistron from both ends of the queue.
  • It is "double ended queue" that's why it is called Deque together with commonly pronounced equally "deck".
  • Deque has dissimilar methods to perform insert, delete together with bear witness the chemical cistron inward queue.
  • Each of these methods convey 2 dissimilar forms. One volition throw an exception if it fails during performance together with other volition furnish a particular value similar naught or false.
Deque is 1 of the collection interface which extends Queue interface Java Deque Interface

Different methods of Deque interface are described below.

  • boolean add(E e) : It volition insert specified chemical cistron at the tail of this deque if infinite is currently available. Return truthful if performance is success or throw an IllegalStateException if fails to perform operation.
  • void addFirst(E e) : It volition insert specified chemical cistron at the get-go of the queue.
  • void addLast(E e) : It volition insert specified chemical cistron at the destination of the queue.
  • boolean contains(Object o) : It volition furnish truthful if specified chemical cistron is available inward queue.
  • E element() : It volition yell back head(first) chemical cistron from queue.
  • E getFirst() : It volition yell back inaugural of all chemical cistron from queue.
  • E getLast() : It volition yell back concluding chemical cistron from queue.
  • boolean offer(E e) : It volition insert specified chemical cistron at the tail of this deque. It volition furnish truthful if success together with faux if performance fails.
  • boolean offerFirst(E e) : It volition insert specified chemical cistron at the get-go of queue if infinite is available.
  • boolean offerLast(E e) : It volition insert specified chemical cistron at the destination of queue if infinite is available.
  • E peek() : It volition yell back head(first) chemical cistron from queue. Returns naught if queue is empty.
  • E peekFirst() : It volition yell back inaugural of all chemical cistron from queue. Returns naught if queue is empty.
  • E peekLast() : It volition yell back concluding chemical cistron from queue. Returns naught if queue is empty.
  • E poll() : It volition take head(first) chemical cistron from queue. Returns naught if queue is empty.
  • E pollFirst() : It volition take inaugural of all chemical cistron from queue. Returns naught if queue is empty.
  • E pollLast() : It volition take concluding chemical cistron from queue. Returns naught if queue is empty.
  • E pop() : It volition pops the chemical cistron from queue.
  • void push(E e) : It volition pushes the chemical cistron onto queue. It volition furnish truthful on success together with throe an exception if performance fails.
  • E remove() : It volition take inaugural of all chemical cistron from queue.
  • boolean remove(Object o) : It volition take inaugural of all occurrence of the given chemical cistron from queue.
  • E removeFirst() : It volition take inaugural of all chemical cistron from queue.
  • boolean removeFirstOccurrence(Object o) : It volition take inaugural of all occurrence of the given chemical cistron from queue.
  • E removeLast() : It volition take concluding chemical cistron from queue.
  • boolean removeLastOccurrence(Object o) : It volition take concluding occurrence of the given chemical cistron from queue.
  • int size() : It volition furnish size of queue.
I convey prepared representative of ArrayDeque to explore you lot how dissimilar methods of Deque works. Also you lot tin work LinkedList() using Deque.

Deque Example

package JAVAExamples;  import java.util.ArrayDeque; import java.util.Deque;  world flat DequeExample {   world static void main(String[] args) {   //Create ArrayDeque.   Deque<String> d = novel ArrayDeque<String>();   //Add elements inward 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 chemical cistron at inaugural of all of Deque.   d.addFirst("First Added");   System.out.println("Deque elements afterwards addFirst are : "+d);      //Add chemical cistron at concluding of Deque.   d.addLast("Last Added");   System.out.println("Deque elements afterwards addLast are : "+d);      //Get inaugural of all chemical cistron from Deque.   System.out.println("First chemical cistron inward deque is : "+d.getFirst());      //Get concluding chemical cistron from Deque.   System.out.println("Last chemical cistron inward deque is : "+d.getLast());      //Get inaugural of all chemical cistron from Deque using peek.   System.out.println("First chemical cistron inward deque using peek is : "+d.peek());      //Get concluding chemical cistron from Deque using peekLast.   System.out.println("Last chemical cistron inward deque using peekLast is : "+d.peekLast());      //Remove inaugural of all chemical cistron from deque.   d.removeFirst();   System.out.println("Deque elements afterwards removeFirst are  : "+d);      //Using pop.   d.pop();   System.out.println("Deque elements afterwards popular are  : "+d);      //Using push.   d.push("First");   System.out.println("Deque elements afterwards force are  : "+d);      //Remove LastOccurrence of chemical cistron from deque using removeLastOccurrence.   d.removeLastOccurrence("four");   System.out.println("Deque elements afterwards 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 afterwards addFirst are : [First Added, one, two, three, four, five, four, six] Deque elements afterwards addLast are : [First Added, one, two, three, four, five, four, six, Last Added] First chemical cistron inward deque is : First Added Last chemical cistron inward deque is : Last Added First chemical cistron inward deque using peek is : First Added Last chemical cistron inward deque using peekLast is : Last Added Deque elements afterwards removeFirst are  : [one, two, three, four, five, four, six, Last Added] Deque elements afterwards popular are  : [two, three, four, five, four, six, Last Added] Deque elements afterwards force are  : [First, two, three, four, five, four, six, Last Added] Deque elements afterwards removeLastOccurrence are  : [First, two, three, four, five, six, Last Added] Size of Deque is  : 7

Related Posts