[Java] Detailed explanation of the use of Java deque

Deque is a double-ended queue interface, inherited from the Queue interface. The implementation classes of Deque are LinkedList, ArrayDeque, LinkedBlockingDeque, among which LinkedList is the most commonly used.

For an introduction to Queue, please see the previous article: Detailed explanation of the use of Queue in Java

Deque has three uses:

  • Ordinary queue (one end goes in and the other goes out):
    Queue queue = new LinkedList()orDeque deque = new LinkedList()
  • Deque (both ends can enter and exit)
    Deque deque = new LinkedList()
  • Stack
    Deque deque = new LinkedList()

Note: The Java stack Stack class is outdated. Java officially recommends using Deque instead of Stack. Deque stack operation methods: push(), pop(), peek().

Deque is a linear collection that supports inserting and removing elements at both ends. The name deque is an abbreviation of "double ended queue" and is usually pronounced as "deck". Most Deque implementations do not have a fixed limit on the number of elements they can contain, but this interface supports both deques with a capacity limit and deques without a fixed size limit.

This interface defines methods for accessing elements at both ends of the deque. Provides methods for inserting, removing, and inspecting elements. Each method has two forms: one form throws an exception when the operation fails, and the other form returns a special value (null or false, depending on the operation). The latter form of insert operation is designed for use with capacity-limited Deque implementations; in most implementations, the insert operation cannot fail.

The following table summarizes the above 12 methods:

  The first element (head) The last element (tail)
  Throw an exception Special value Throw an exception Special value
insert addFirst(e) offerFirst(e) addLast(e) offerLast (e)
delete removeFirst() pollFirst() removeLast() pollLast()
an examination getFirst() peekFirst() getLast() peekLast ()

The Deque interface extends (inherited) the Queue interface. When using a deque as a queue, you will get FIFO (first in first out) behavior. Add elements to the end of the deque and remove elements from the beginning of the deque. The methods inherited from the Queue interface are completely equivalent to the Deque methods, as shown in the following table:

Queue method Equivalent Deque method
add add(e) addLast(e)
offer(e) offerLast (e)
remove() removeFirst()
poll() pollFirst()
element() getFirst()
peek() peekFirst()

The deque can also be used as a LIFO (Last In First Out) stack. This interface should be used in preference to the legacy Stack class. When using a deque as a stack, elements are pushed into the beginning of the deque and popped from the beginning of the deque. The stack method is completely equivalent to the Deque method, as shown in the following table:

Stack method Equivalent Deque method
push(e) addFirst(e)
pop() removeFirst()
peek() peekFirst()

Guess you like

Origin blog.csdn.net/qq_43737121/article/details/111562756
Recommended