
Deque, Queue and Stack in java
java is defined in Java. The util.Queueinterface is used to represent queues. Queue in Java belongs to the same level interface as List and Set, they all inherit from CollectionInterface. Java also defines a double-ended queue java.util.Deque, our commonly used LinkedList is implemented Dequeinterface. From the above figure we can know that “Queue has a direct subclass PriorityQueue” and Deque has two direct subclasses “LinkedList and ArrayDeque”. public interface Queue extends Collection {boolean add(E e);boolean offer(E e);E remove();E poll();E element() ;E peek();} public interface Deque extends Queue {void addFirst(E e);void addLast(E e); boolean offerFirst(E e);boolean offerLast(E e);E removeFirst();E removeLast();E pollFirst();E pollLast();E getFirst();E getLast();E peekFirst();E peekLast();boolean removeFirstOccurrence(Object o);boolean removeLastOccurrence(Object o);// *** Queue methods ***boolean add(E e);boolean offer(E e);E remove();E poll( );E element();E peek();// *** Stack methods ***void push(E e);E pop();// *** Collection methods ***boolean remove(Object o) ;boolean contains(Object o);public int size();Iterator iterator();Iterator descendingIterator();} From the official explanation, ;ArrayDeque is a double-ended queue with no initial capacity; LinkedList is a two-way linked list. And we can also see that “ArrayDeque is more efficient than LinkedList when used as a queue” and in the stack usage scenario, “LinkedList, which has a tail node that does not need to be empty, is undoubtedly more…

Java collection List, Set, Map, Queue, Deque
Collections are a very important part of the foundation of Java,Java provides a very rich collection API,Understand the characteristics of each collection,How to use the correct collection in various scenarios “Very important” is also the most basic quality of a Java programmer. Overall understanding The most basic and most commonly used collections in Java are mainly “Set” and “List” ;Map,Queue,Deque. Collection is the top-level interface of the collection List,Set,Queue. Collection inherits from the Iterable interface to implement traversal of elements. The relationship between each class or interface is as shown in the following class diagram: List List is an ordered collection interface, inherited from Collection, has three implementation classes,ArrayList,LinkedList, ;Vector, each has its own characteristics. ArrayList,The bottom layer is implemented through arrays,Elements have subscripts,Randomly reading elements through subscripts is highly efficient,The query time complexity is O(1). The disadvantage is that “when inserting or deleting elements”, “need to move elements” or when the array storage space is insufficient, “array expansion” is relatively expensive. In summary, ArrayList is suitable for finding and traversing elements but not for inserting and deleting elements. And ArrayList is thread-unsafe. LinkedList,The bottom layer is implemented through a linked list,Elements have no subscripts,Suitable for dynamic insertion and deletion…