1. Interface definition
This is a collection of elements designed to hold before processing. In addition to the basic java.util.Collection operations, queues provide additional insert, extract, and retrieve operations. Each of these operation methods comes in one of two forms: either to throw an exception when the operation fails, or to return a special value (either null or false, depending on the operation). The latter form of insert operation is specifically designed for use with a capacity-constrained Queue implementation; In most implementations, the insert operation does not fail.
– | An exception is thrown | Return special value |
---|---|---|
insert | add(e) | offer(e) |
Extraction/Removal | remove() | poll() |
Search/Check | element() | peek() |
Queues usually (but not necessarily) sort elements in a FIFO (first in, first out) manner. The exception is the priority queue, which sorts the elements according to the natural order of the provided comparators or elements; There is also a LIFO (last in, first out) queue (or stack), which sorts elements by LIFO. However, regardless of the order in which the queue is used, its header element is the element that will be removed by calling remove() or poll(). In a FIFO queue, all new elements are inserted at the end of the queue; other types of queues may use different placement rules. Each Queue implementation must specify its own sorting properties.
The offer() method inserts an element if possible, otherwise returns false. This differs from the collection.add () method, which can only indicate that adding an element failed by throwing an unchecked exception. The offer() method is designed for situations where “failure is normal, not exceptional,” such as in a fixed-capacity (or “bounded”) queue.
The remove() and poll() methods remove and return the head of the Queue. Which element to remove from the Queue is specified by a function of the Queue sorting policy, which varies from Queue implementation to Queue implementation. The remove() and poll() methods behave differently only if the queue is empty: the remove() method throws an exception, while the poll() method returns null.
The element() and peek() methods return, but do not remove, the queue header element.
The java.util.Queue interface does not define a method for blocking queues, which is common in concurrent programming. These elements appear or waiting for the space available methods in Java. The util. Concurrent. BlockingQueue interface is defined in this interface inherits the Queue interface.
Queue implementations generally do not allow the insertion of null elements, although some (such as LinkedList) do not. Even in an implementation that allows it, a null value should not be inserted into a Queue, because null is also used as a special return value by the poll() method to indicate that the Queue contains no elements.
Queue implementations typically do not define element-based versions of equals() and hashCode(), but instead inherit identity-based versions of methods from class Object, because element-based equality is not always well-defined for queues that have the same elements but different sorted attributes.
The java.util.Queue interface inherits from the java.util.Collection interface, which in turn inherits from the java.lang.Iterable interface, as shown in the figure below:
2. Method declarations
1) Boolean add(E E);
- If the specified element can be inserted into the queue immediately without violating the capacity limit, it is returned on successful insertion
true
(e.g.,Collection.add()
Designated); - Thrown if the element cannot be added at this point due to capacity constraints (no free space)
java.lang.IllegalStateException
; - Thrown if the class of the specified element prevents it from being added to this queue
java.lang.ClassCastException
; - If the specified element is
null
And the queue is not allowednull
Element is thrownjava.lang.NullPointerException
; - Thrown if some attribute of this element prevents it from being added to this queue
java.lang.IllegalArgumentException
.
2) Boolean offer(E E);
- If the specified element can be inserted into the queue immediately without violating the capacity limit, it is returned on successful insertion
true
, otherwise returnfalse
. This method is usually better than that when using a limited capacity queueadd()
Better to use, because when an element cannot be added due to capacity constraints (no free space),add()
Methods can only throw exceptions; - Thrown if the class of the specified element prevents it from being added to this queue
java.lang.ClassCastException
; - If the specified element is
null
And the queue is not allowednull
Element is thrownjava.lang.NullPointerException
; - Thrown if some attribute of this element prevents it from being added to this queue
java.lang.IllegalArgumentException
.
3) E the remove ();
- Retrieves and removes the header element of this queue
poll()
The difference is that if the queue is empty, an exception is thrown. - If the queue is empty, it is thrown
java.util.NoSuchElementException
.
4) E poll ();
- Retrieves and removes the header element of this queue, and returns if the queue is empty
null
.
5) E element ();
- Retrieves (but does not remove) the header element of this queue, which is similar to
peek()
The difference is that if the queue is empty, an exception is thrown. - If the queue is empty, it is thrown
java.util.NoSuchElementException
.
6) E peek ();
- Retrieves (but does not remove) the header element of this queue, and returns if the queue is empty
null
.