Menu
Topics Index
...
`
Deque is an interface which supports element insertion and removal at both ends. The name deque is short for "double ended queue" and is usually pronounced as "deck".

Dequeu interface defines methods to access the elements at both ends of the deque. The following are the methods defined by Dequeu interface.
Method Description
add(E e) Inserts the specified element at the tail of this deque returning true upon success and throwing an IllegalStateException if no space is currently available.
addFirst(E e) Inserts the specified element at the front of this deque if it is possible to do so immediately without violating capacity restrictions.
addLast(E e) Inserts the specified element at the end of this deque if it is possible to do so immediately without violating capacity restrictions.
contains(Object o) Returns true if this deque contains the specified element.
descendingIterator() Returns an iterator over the elements in this deque in reverse sequential order.
element() Retrieves, but does not remove, the head of the queue represented by this deque (in other words, the first element of this deque).
getFirst() Retrieves, but does not remove, the first element of this deque.
getLast() Retrieves, but does not remove, the last element of this deque.
iterator() Returns an iterator over the elements in this deque in proper sequence
offer(E e) Inserts the specified element at the tail of the deque if the element is added true is returned else false is returned.
offerFirst(E e) Inserts the specified element at the front of this deque unless it would violate capacity restrictions.
offerLast(E e) Inserts the specified element at the end of this deque unless it would violate capacity restrictions.
peek() Retrieves, but does not remove, the head of the queue represented by this deque (in other words, the first element of this deque), or returns null if this deque is empty.
peekFirst() Retrieves, but does not remove, the first element of this deque, or returns null if this deque is empty.
peekLast() Retrieves, but does not remove, the last element of this deque, or returns null if this deque is empty.
poll() Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque), or returns null if this deque is empty.
pollFirst() Retrieves and removes the first element of this deque, or returns null if this deque is empty.
pollLast() Retrieves and removes the last element of this deque, or returns null if this deque is empty.
pop() Pops an element from the stack represented by this deque.
push(E e) Pushes an element onto the stack represented by this deque (in other words, at the head of this deque) if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available.
remove() Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque).
remove(Object o) Removes the first occurrence of the specified element from this deque.
removeFirst() Retrieves and removes the first element of this deque.
removeFirstOccurrence(Object o) Removes the first occurrence of the specified element from this deque
removeLast() Retrieves and removes the last element of this deque.
removeLastOccurrence(Object o) Removes the last occurrence of the specified element from this deque.
size() Returns the number of elements in this deque.
The classes which implement Deque interface are ArrayDeque, ConcurrentLinkedDeque, LinkedBlockingDeque, LinkedList
DequeDemo
class DequeDemo
{
    Deque dq = new ArrayDeque<String>();
    dq.add("C++");
    dq.add("C#");
    dq.add("Java");
    System.out.println("Till now elements in dequeu : " + dq);
    // C added to first
    dq.addFirst("C"); // LINE A
    System.out.println("deque after adding C at first: " + dq);
    // Now FORTRAN is added first
    dq.addFirst("FORTRAN"); // LINE B
    System.out.println("deque after  adding FORTRAN at first : " + dq);
    // Now removing from deque
    //Removing Java from deque
    dq.remove("Java"); // LINE C
    System.out.println("deque after removing Java : " + dq);
    //Removing FirstElement
    dq.removeFirst(); // LINE D
    System.out.println("deque after removing first element : " + dq);
    dq.removeLast(); // LINE E
    System.out.println("deque after removing last element : " + dq);
}
OUTPUT

Till now elements in dequeu : [C++, C#, Java]
deque after adding C at first: [C, C++, C#, Java]
deque after  adding FORTRAN at first : [FORTRAN, C, C++, C#, Java]
deque after removing Java : [FORTRAN, C, C++, C#]
deque after removing first element : [C, C++, C#]
deque after removing last element : [C, C++]

DESCRIPTION

In the above program we have created a deque and added elements to it. At LINE A we added C to the first of deque. At LINE B we added FORTRAN to the first of deque At LINE C we removed Java from deque. At LINE D we have removed first element in the deque. At LINE E we have removed last element from deque.

THINGS TO TRY
  • Add SQL to the first of deque and Ruby to the last of deque using offerFirst and offerLast methods and print the deque.
    dq.offerFirst("SQL");
    dq.offerLast("Ruby");
    System.out.println(dq);
  • Remove first element from deque and print deque
    System.out.println("Removed first element from deque : " + dq.pop() + "\n" + dq);
  • Check whether Pearl is in deque
    System.out.println("deque contains Pearl : " + dq.contains("Pearl"));

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App