Java Deque Interface

www.igi‮c.aeditf‬om

In Java, the Deque interface is a part of the Java Collections Framework that provides a double-ended queue, which is a data structure that allows adding and removing elements from both ends. The name "Deque" is short for "Double-Ended Queue."

The Deque interface is an extension of the Queue interface, which means that it inherits all the methods of the Queue interface, but it also adds methods for inserting and removing elements from both ends of the deque. Here are some of the key methods defined in the Deque interface:

  • void addFirst(E element): Inserts the specified element at the beginning of the deque.

  • void addLast(E element): Inserts the specified element at the end of the deque.

  • boolean offerFirst(E element): Inserts the specified element at the beginning of the deque and returns true if successful.

  • boolean offerLast(E element): Inserts the specified element at the end of the deque and returns true if successful.

  • E removeFirst(): Removes and returns the first element of the deque.

  • E removeLast(): Removes and returns the last element of the deque.

  • E pollFirst(): Removes and returns the first element of the deque or returns null if the deque is empty.

  • E pollLast(): Removes and returns the last element of the deque or returns null if the deque is empty.

  • E getFirst(): Returns the first element of the deque without removing it.

  • E getLast(): Returns the last element of the deque without removing it.

  • E peekFirst(): Returns the first element of the deque without removing it, or returns null if the deque is empty.

  • E peekLast(): Returns the last element of the deque without removing it, or returns null if the deque is empty.

The Deque interface is implemented by several classes in the Java Collections Framework, such as ArrayDeque and LinkedList. These classes provide different implementations of the deque data structure with varying performance characteristics.

The Deque interface is useful for implementing algorithms that require adding and removing elements from both ends of a data structure, such as the implementation of a stack or a queue.