The other two branches of collection: list and set have been analyzed in the previous chapter to analyze the underlying implementation of the queue.
Top three articles about the Java container class:
Java Container class 1:collection,list,arraylist,linkedlist in-depth interpretation of Java container Classes 2:map and HashMap in-depth understanding of Java container Classes 3:set/hastset/mapset in-depth understanding of queue
 Public Interface extends collection<e> {    boolean Add (E var1);    Boolean offer (E var1);    E remove ();    E poll ();    E element ();    E peek ();}
This is the code for the queue interface, which is much simpler and more straightforward than the list or set. The following describes the meaning of the interface inside it:
CollectionOperation, the queue also provides additional
Insert, extract, and check
Operation. There are two forms of each method:
One throws an exception (when the operation fails), and the other Returns a special value (null or false, depending on the action).
The Queue implements the design; In most implementations, the insert operation does not fail.
Abstractqueue
Abstractqueue in the implementation of the queue and collection part of the function, relatively simple, the source code is as follows:
 Public Abstract class extends Abstractcollection<e>    implements queue<e> {
    protected Abstractqueue () {    }
     Public BooleanAdd (e e) {if(Offer (E))return true;Else            Throw NewIllegalStateException ("Queue Full"); } PublicE Remove () {e x = poll ();if(X! =NULL)returnXElse            Throw NewNosuchelementexception (); } Publice element () {e x = peek ();if(X! =NULL)returnXElse            Throw NewNosuchelementexception (); } Public voidClear () { while(Poll ()! =NULL)            ; } Public BooleanAddAll (collection<?extendsE> c) {if(c = =NULL)Throw NewNullPointerException ();if(c = = This)Throw NewIllegalArgumentException ();BooleanModified =false; for(E e:c)if(Add (e)) modified =true;returnModified }}
As can be seen from the above invocation relationship, the queue's interpretation is the one that throws the exception, and which is the calling interface that does not throw the exception.
Deque
A linear collection that supports inserting and removing elements at both ends. The name deque is the abbreviation for double ended queue (double-ended queues) , which is usually read as "deck". Most Deque implementations have no fixed limit on the number of elements they can contain, but this interface supports both a capacity-constrained double-ended queue and a double-ended queue without a fixed size limit. (In the Java 1.6 version of the home buckle, 1.8 in the interface changes, but probably the meaning of similar)
The LinkedList is introduced in Java Container Class 1, and the linked list class actually implements the Deque interface, so the list supports adding and removing elements from the head and tail.
Arraydeque
Because the storage structure of the linked list may be relatively simple, here is a description of arraydeque, which stores elements using an array. As an array of double-ended queues, the logic involved in scaling and copying elements may be more complex.
Take a look at the several constructors inside:
 Public Arraydeque () {        
 new object[16];
    } public arraydeque (int numelements) {        
allocateelements (numelements);
    }Publicextends e> c) {        allocateelements (c.size ());        AddAll (c);    }
From the constructor you can see that the default is to allocate an array of length 16. At the same time, a queue of the specified size is supported (allocateelements function here before the
The Java container class 2:map and HashMap in-depth analysis has been thoroughly analyzed, is a very delicate function). Let's see how the insertion is implemented. And how do you automatically expand the array?
Two member variables are maintained in Arrayqueue: Head and tail each represent the header and tail of the queue in the array subscript.
/** * The index of the element at the head of the      deque (which was the     * element that would was removed by Remov E () or pop ()); A     * arbitrary number equal to tail if the deque is empty.     */    transientint head;    /**     * The index at which the next element would is added to the tail     * of the Deque (via AddLast (e), add (E), or push (E)).     */    transientint tail;
Add elements at the header of the queue:
 Public void AddFirst (e e) {        ifnull)            thrownew nullpointerexception ();        
Elements[head = (head-1) & (elements.length-1)] = e;
        if (head = = tail)            
doublecapacity ();
    }
Public void
AddLast (e e) {
    
if
NULL
)
        
throw New
NullPointerException ();
    Elements[tail] = e;
    
if
((tail = (tail + 1) & (elements.length-1)) = = head)
        Doublecapacity ();
}
Functions assigned by constructors and arrays can be known that the length of an array is definitely an integer of a power of 2.
When head is an integer greater than 0, it is easy to insert the first element of head into E when the header is inserted. So when head is 0 o'clock, how is it calculated? It can be seen from above that the end of the array is inserted. So Arraydeque is equivalent to a ring, which specifies a first tail as the front and back of the queue (connecting the first of the array).
The principle of adding elements at the last position is similar to adding in the header. Pay attention to judging whether the judgment is full, and this is no longer analyzed.
When the queue is full, the length of the array is double. Since arrays are not free to expand, the Doublecapacity function should be to allocate a larger array and copy the original elements into it, which is no longer analyzed.
In general, the double-ended queue Arraydeque is implemented on the basis of the array, the principle and implementation are not complicated, but many of the details of boundary adjustment can be considered.
Blockingqueue
Blockingqueue is the concurrent package below, the follow-up intends to write a series of articles specifically analyzing the concurrent package under the class, and some multithreading related things.
Priorityqueue
The priority queue is a queue that can be sorted. The interior is a maximum heap, and most people should know about heap ordering, so it should be no stranger to the largest heap.
Each read element is the most read element (by default).
The external interface has the following:
 
 
  
   
   | Method Name | function Description | 
 
  
  
   
   | Add (e E) | adding elements | 
 
   
   | Clear () | Empty | 
 
   
   | Contains (Object o) | Checks whether the current parameter element is included | 
 
   
   | Offer (e e) | adding elements | 
 
   
   | Peek () | Read elements, (not deleted) | 
 
   
   | Poll () | remove element, (delete) | 
 
   
   | Remove (Object o) | Delete the specified element | 
 
   
   | Size () | return length | 
 
  
Priorityqueue default is a maximum heap structure, if you want to construct a minimal heap:
Private Static Final int default_initial_capacity = 11; priorityqueue<integer> maxheap=Newnew comparator<integer> () {        @Override         Public int Compare (integer O1, integer o2) {            return o2-o1;        }    });
About the structure of the heap part of the analysis here no longer can be consulted: https://www.cnblogs.com/tstd/p/5125949.html
Priority Queue Analysis for C + +: A detailed description of priority queue usage (priority_queue)
Because the data is saved through an array, the priority queue also involves capacity expansion, and the hashmap/setting/collection is the same as the expansion principle, or even simpler, no longer analyzed. Priorityqueue internal operations are carried out on the basis of the largest heap, reading the data structure of the heap data can be understood.
Reference:
Http://tool.oschina.net/uploads/apidocs/jdk-zh/java/util/Deque.html
Http://www.cnblogs.com/NeilZhang/p/5650226.html
Java Container class 4:queue in-depth interpretation