I. Overview
A queue structure collection that stores the elements that will be processed. Typically, the elements are sorted in a FIFO manner, but this is not required. For example, the priority queue is an exception, which is sorted by the value of the element. But anyway, The implementation of each queue must specify its sorting properties. The queue typically does not define the equal and Hashcode methods of the element.
Two. Main methods
There are two forms of each queue method (1) The failure of an operation throws an exception (2) The operation fails to return a specific value, usually null or FALSE
Type of operation |
Throw exception |
return a specific value |
Insert |
Add (E) |
Offer (e) |
Remove |
Remove () |
Poll () |
Examine |
Element () |
Peek () |
The implementation of the queue may limit the number of elements stored by the collection, which is called a bounded queue, and for bounded queues when the Add method is called, the IllegalStateException exception is thrown if the number of elements exceeds its capacity limit. Offer method is specially designed for the bounded queue , and add is different when the insertion element fails, it returns false instead of throwing an exception.
Both the Remove and poll methods eject elements from the head of the queue. Specifically, the element is ejected, which depends on the queue's sorting strategy. Remove and poll only differ when it is an empty collection, and remove throws Nosuchelementexception, Instead, poll returns a null value.
Both the element and Peek methods return the header elements of the queue, but they are not removed from the queue. Same as remove and poll only when the empty collection is different, element throws Nosuchelementexception and peek returns a null value .
The implementation of the queue usually does not allow the insertion of null values, except for the exception of LinkedList, which allows the insertion of null values for historical reasons, but it is important to note that NULL is also the special value returned by the poll and Peek methods.
Three. Implement
The implementation of queue can be divided into common implementation and concurrency implementation
The general implementation of the main two, one is LinkedList, one is priorityqueue.linkedlist in the list we have said, it inherits from the queue interface, provides a FIFO queue operation form.
Priorityqueue is a stack-based prioritization queue that can be sorted according to the natural ordering of elements or a given sequencer.
A series of synchronous queue interfaces and real classes are included in the Java.util.concurrent package. Blockingqueue inherits from the queue, which waits for a non-empty queue to be available when retrieving elements, and changes the state to a usable state after an element is deposited. The following is its implementation class.
- L Inkedeblockingqueue -optional bounded FIFO blocking queue based on link node
- arrayblockingqueue -array-based, bounded FIFO-style blocking queue
- priorityblockingqueue -based on the stack structure unbounded blocking queue
- Span style= "FONT-SIZE:16PX; Font-family: Young Round "> delayqueue - Time dispatch queue based on stack structure
- sychronousqueue -queue by using a simple docking mechanism of the Blockingqueue interface
In JDK7, Transferqueue is a special blockingqueue that, after adding an element to the queue, can choose to be in a wait (blocking) state to have another thread retrieve the element, Transferqueue only one implementation class
- linkedtransferqueue-unbounded transferqueue based on link nodes
Java Collection Framework (14) Queue