Queues are an important part of the Java collection, with first-out features that allow for a wide range of application scenarios, such as queuing. So let's learn about queues in Java today. Examples of this article use the JAVA8 environment.
Inheriting class diagrams
The learning queue, first to know its class inheritance system, know each kind of queue to implement which interfaces, which inherits the class, this helps us to understand. The following is a class inheritance diagram for the Java8 Squadron column.
As can be seen from the Inheritance class diagram, the queue is divided into two main, a non-blocking queue, the implementation of the queue interface, including Linkedlist,arraydeque and priorityqueue; one is blocking the queue, realizing the Blockingqueue, including Arrayblockingqueue,linkedblockingqueue,synchronousqueue and Linkedblockingdeque.
iterable Interface
The class that implements the Iterable interface has an iterative function that can be used for "For-each" loop statements. The interface has the following methods:
 Public Interface Iterable<t> {    Iterator<T> Iterator ();     default void Super T> action) {        objects.requirenonnull (action);          for  This ) {            action.accept (t);        }    }     default Spliterator<t> Spliterator () {        return spliterators.spliteratorunknownsize ( Iterator (), 0);}    }
Queue Interface
The queue interface represents queues, deleting elements at the head of a team, inserting elements at the end of a team, having first-in, FIFO features, and the method of interface declarations . 
1  Public InterfaceQueue<e>extendsCollection<e> {2     3     //inserts an element to the end of the queue, returns true successfully, fails to return false, throws an exception with insufficient space4     BooleanAdd (e e);5 6     //insert element to end of queue, success returns TRUE, Failure returns false7     BooleanOffer (e e);8 9     //Remove and return the team header element, throw an exception if the queue is emptyTen E Remove (); One  A     //removes and returns the team header element, or null if the queue is empty - E poll (); -  the    //returns the team header element, which throws an exception if the queue is empty - E Element (); -  -     //returns the team header element, or null if the queue is empty + E Peek (); -}
Deque Interface
Deque is a double-ended queue, the abbreviation for a double end queue, and the queue can only delete elements at the head of the queue, and the end-of-line insert elements are different, deque can insert and delete elements separately at the tail of the queues. Because it is possible to insert at the same end and
Deletes the element so it can be used as a stack. The following is the method that the interface declares:
 Public InterfaceDeque<e>extendsQueue<e> {    /*** Insert element in team header, if queue is limited to capacity, throw exception when capacity is insufficient * illegalstatexception*/    voidAddFirst (e e); /*** Inserts an element at the end of the queue, and throws an exception when the capacity is low if the queues are limited to capacity * Illegalstatexception*/    voidAddLast (e e); /*** Insert element in team header, insert successful return true, Failure returns false * This method is best used if the queue is limited to capacity*/    BooleanOfferfirst (e e); /*** Insert element at end of team, return true successfully, fail return false; * If the queue is limited in capacity, it is best to use this method*/    BooleanOfferlast (e e); /*** Returns and removes elements from the team header and throws an exception if the queue is empty*/E Removefirst (); /*** Returns and removes the element at the end of the queue and throws an exception if it is empty*/E removelast (); /*** Returns and removes elements from the team header, or null if the queue is empty*/E Pollfirst (); /*** Returns and removes the element at the end of the queue, or null if the queues are empty*/E polllast (); /*** Returns the element of the team header, throws an exception if the queue is empty nosuchelementexception*/E GetFirst (); /*** Returns the element at the end of the queue, and throws an exception if it is empty nosuchelementexception*/E getlast (); /*** Returns the element of the team header, or null if the queue is empty*/E Peekfirst (); /*** Returns the element at the end of the queue, or null if the queues are empty*/E peeklast (); Booleanremovefirstoccurrence (Object o); Booleanremovelastoccurrence (Object o); //* * * Queue methods * * *    BooleanAdd (e e); BooleanOffer (e e);    E Remove ();    E poll ();    E element ();    E Peek (); //* * * Stack methods * * *    voidpush (e E);    E pop (); //* * * * Collection methods * * *    BooleanRemove (Object o); Booleancontains (Object o);  Public intsize (); Iterator<E>iterator (); Iterator<E>descendingiterator ();}
Blockingqueue Interface
Blockingqueue is the interface provided by the Java.util.concurrent package, which indicates that the blocking queue differs from the normal queue in that when the element is fetched from the team header, if the queue is empty, the blocking queue blocks until there are available elements, waits for timeouts, or is interrupted;
When an element needs to be inserted at the end of a queue, if there is no space available, the operation blocks until there is free space, waits for a timeout, or is interrupted. The following is the method that the interface declares:
 Public InterfaceBlockingqueue<e>extendsQueue<e> {        BooleanAdd (e e); BooleanOffer (e e); /*** Insert Element, if there is not enough space, will block until there is free space*/    voidPut (e e)throwsinterruptedexception; /*** Insert element, if insufficient capacity, will block until there is free space, or wait for timeout*/    BooleanOffer (e E,LongTimeout, timeunit unit)throwsinterruptedexception; /*** Returns and removes elements from the team header, if the queue is empty, wait **/E take ()throwsinterruptedexception; /*** Returns and removes the element of the team header if the queue is empty, blocks until there are available elements, or waits for timeout **/E Poll (LongTimeout, timeunit unit)throwsinterruptedexception; /*** Returns how many elements can be stored in the queue * This method will not be blocked, return directly*/    intremainingcapacity (); /*** Delete the given element if there are multiple in the queue for the given element, delete only the first * successful Delete, return true, otherwise, return false*/    BooleanRemove (Object o); /*** If a given element exists in the queue, there is a return of true, otherwise false*/     Public Booleancontains (Object o); /*** Remove all elements from the queue and add them to the given container C * This method is more efficient than looping the poll method*/    intDrainto (collection<?SuperE>c); /*** Remove up to maxelements elements from the queue and add them to container C*/    intDrainto (collection<?SuperE> C,intmaxelements);}
Blockingdeque Interface
Blockingdeque is a double-ended blocking queue, where you can insert and delete elements at the head and tail of a queue, which can be used as a blocking stack, following the method that the interface declares:
 Public InterfaceBlockingdeque<e>extendsBlockingqueue<e>, deque<e> {    voidAddFirst (e e); voidAddLast (e e); BooleanOfferfirst (e e); BooleanOfferlast (e e); voidPutfirst (e E)throwsinterruptedexception; voidPutlast (e E)throwsinterruptedexception; BooleanOfferfirst (e E,LongTimeout, timeunit unit)throwsinterruptedexception; BooleanOfferlast (e E,LongTimeout, timeunit unit)throwsinterruptedexception; E Takefirst ()throwsinterruptedexception; E takelast ()throwsinterruptedexception; E Pollfirst (LongTimeout, timeunit unit)throwsinterruptedexception; E Polllast (LongTimeout, timeunit unit)throwsinterruptedexception; Booleanremovefirstoccurrence (Object o); Booleanremovelastoccurrence (Object o); //* * * * blockingqueue methods * * *    BooleanAdd (e e); BooleanOffer (e e); voidPut (e e)throwsinterruptedexception; BooleanOffer (e E,LongTimeout, timeunit unit)throwsinterruptedexception;    E Remove ();    E poll (); E Take ()throwsinterruptedexception; E Poll (LongTimeout, timeunit unit)throwsinterruptedexception;    E element ();    E Peek (); BooleanRemove (Object o);  Public Booleancontains (Object o);  Public intsize (); Iterator<E>iterator (); //* * * Stack methods * * *    voidpush (e e);}
Java Queue Learning