Java Deque and Its Application

1. The concept of Deque

Two-way queue: supports a linear collection of elements inserted and deleted at the beginning and end. It has both FIFO (first in first out) and LIFO (last in first out) characteristics, that is, it is a queue and a stack ; the
official java document recommends using deque to implement the stack (stack).

Insert picture description here

In Java, the implementation class of Queue generally uses LinkedList.
Features:

  • 1. Insert, delete, and get operations support two forms: fail fast and return null or true/false
  • 2. It has both FIFO and LIFO characteristics, which is both a queue and a stack
  • 3. It is not recommended to insert a null element, null as a specific return value indicates that the queue is empty
  • 4. Undefined equals and hashCode based on element equality

2. Deque operation

Insert element

Method name description
addFirst() Insert an element to the head of the line, if the element is empty, a null pointer exception occurs
addLast() Insert an element to the end of the queue, if it is empty, a null pointer exception occurs
offerFirst() Insert an element to the head of the line, if the insertion is successful, return true, otherwise return false
addLast() Insert an element to the end of the line, if the insertion is successful, return true, otherwise return false

Delete element

Method name description
removeFirst() Return and remove the head element. If the element is null, a NoSuchElementException will occur
removeLast() Return and remove the tail element. If the element is null, a NoSuchElementException will occur
offerFirst() Return and remove the head element, if there is no element in the queue, return null
pollLast() Return and remove the tail element, if there is no element in the queue, return null

Get element

Method name description
getFirst() Get the head element of the queue but do not remove it. If there is no element in the queue, a NoSuchElementException will occur
getLast() Get the last element of the queue but not remove it. If there is no element in the queue, a NoSuchElementException will occur
peekFirst() Get the head element of the queue but do not remove it, if there is no element in the queue, return null
peekLast () Get the tail element but don't remove it, if there is no element in the queue, return null

Stack operation

Method name description
pop() Popping elements in the stack, that is, returning and removing the head element, is equivalent to removeFirst(), if there is no element in the queue, a NoSuchElementException will occur
push() Pushing an element into the stack, that is, adding an element to the head of the line, is equivalent to addFirst(), if the element is null, an NPE occurs, and if the stack space is limited, an IllegalStateException occurs

Judged as empty condition

Method name description
isEmpty() To check whether this deque is "empty" or "non-empty", no exception will be thrown

Main implementation classes:
ArrayDeque: Linear two-way queue based on array implementation
LinkedList: Chain two-way queue based on linked list implementation

3. Demonstration example of deque

public static void main(String[] args) {
    
    
		Deque<Integer> deque=new LinkedList<Integer>();
		deque.offerLast(10);
		deque.offerLast(15);
		deque.offerLast(20);
		deque.offerFirst(5);
		System.out.println("弹出队首元素"+deque.pollFirst());
		System.out.println("弹出队尾元素"+deque.pollLast());
		System.out.println("查看此时队尾元素"+deque.peekLast());
		System.out.println("判断队列是否为空"+deque.isEmpty());
}

Insert picture description here

4. Typical application of deque (sliding window/monotonic stack problem)

Monotonic queue problem
refers to Offer 59-I. Maximum sliding window

Guess you like

Origin blog.csdn.net/qq_39736597/article/details/113815267