Java Deque Interface

  • Deque is 1 of the collection interface which extends Queue interface.
  • Deque is linear collection which allow us to add together together with take away chemical percentage from both ends of the queue.
  • It is "double ended queue" that's why it is called Deque together with normally pronounced every bit "deck".
  • Deque has unlike methods to perform insert, delete together with examine the chemical percentage inwards queue.
  • Each of these methods convey 2 unlike forms. One volition throw an exception if it fails during functioning together with other volition supply a exceptional value similar nada or false.
java deque interface

Different methods of Deque interface are described below.

  • boolean add(E e) : It volition insert specified chemical percentage at the tail of this deque if infinite is currently available. Return truthful if functioning is success or throw an IllegalStateException if fails to perform operation.
  • void addFirst(E e) : It volition insert specified chemical percentage at the showtime of the queue.
  • void addLast(E e) : It volition insert specified chemical percentage at the halt of the queue.
  • boolean contains(Object o) : It volition supply truthful if specified chemical percentage is available inwards queue.
  • E element() : It volition think head(first) chemical percentage from queue.
  • E getFirst() : It volition think foremost chemical percentage from queue.
  • E getLast() : It volition think concluding chemical percentage from queue.
  • boolean offer(E e) : It volition insert specified chemical percentage at the tail of this deque. It volition supply truthful if success together with imitation if functioning fails.
  • boolean offerFirst(E e) : It volition insert specified chemical percentage at the showtime of queue if infinite is available.
  • boolean offerLast(E e) : It volition insert specified chemical percentage at the halt of queue if infinite is available.
  • E peek() : It volition think head(first) chemical percentage from queue. Returns nada if queue is empty.
  • E peekFirst() : It volition think foremost chemical percentage from queue. Returns nada if queue is empty.
  • E peekLast() : It volition think concluding chemical percentage from queue. Returns nada if queue is empty.
  • E poll() : It volition take away head(first) chemical percentage from queue. Returns nada if queue is empty.
  • E pollFirst() : It volition take away foremost chemical percentage from queue. Returns nada if queue is empty.
  • E pollLast() : It volition take away concluding chemical percentage from queue. Returns nada if queue is empty.
  • E pop() : It volition pops the chemical percentage from queue.
  • void push(E e) : It volition pushes the chemical percentage onto queue. It volition supply truthful on success together with throe an exception if functioning fails.
  • E remove() : It volition take away foremost chemical percentage from queue.
  • boolean remove(Object o) : It volition take away foremost occurrence of the given chemical percentage from queue.
  • E removeFirst() : It volition take away foremost chemical percentage from queue.
  • boolean removeFirstOccurrence(Object o) : It volition take away foremost occurrence of the given chemical percentage from queue.
  • E removeLast() : It volition take away concluding chemical percentage from queue.
  • boolean removeLastOccurrence(Object o) : It volition take away concluding occurrence of the given chemical percentage from queue.
  • int size() : It volition supply size of queue.
I convey prepared representative of ArrayDeque to explore you lot how unlike 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 bird DequeExample {   world static void main(String[] args) {   //Create ArrayDeque.   Deque<String> d = novel ArrayDeque<String>();   //Add elements inwards 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 percentage at foremost of Deque.   d.addFirst("First Added");   System.out.println("Deque elements afterwards addFirst are : "+d);      //Add chemical percentage at concluding of Deque.   d.addLast("Last Added");   System.out.println("Deque elements afterwards addLast are : "+d);      //Get foremost chemical percentage from Deque.   System.out.println("First chemical percentage inwards deque is : "+d.getFirst());      //Get concluding chemical percentage from Deque.   System.out.println("Last chemical percentage inwards deque is : "+d.getLast());      //Get foremost chemical percentage from Deque using peek.   System.out.println("First chemical percentage inwards deque using peek is : "+d.peek());      //Get concluding chemical percentage from Deque using peekLast.   System.out.println("Last chemical percentage inwards deque using peekLast is : "+d.peekLast());      //Remove foremost chemical percentage 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 percentage 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 percentage inwards deque is : First Added Last chemical percentage inwards deque is : Last Added First chemical percentage inwards deque using peek is : First Added Last chemical percentage inwards 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


More interesting articles here :Generation Enggelmundus Internet Marketing Tool here :Zeageat IM http://www.software-testing-tutorials-automation.com/
Post a Comment (0)
Previous Post Next Post