java.util
public
interface
java.util.Queue<E>
A kind of collection provides advanced operations than other basic
collections, such as insertion, extraction, and inspection.
Generally, a queue orders its elements by means of first-in-first-out. While
priority queue orders its elements according to a comparator specified or the
elements' natural order. Furthermore, a stack orders its elements
last-in-first out.
A typical queue does not allow null to be inserted as its element, while some
implementations such as LinkedList allow it. But null should not be inserted
even in these implementations, since method poll return null to indicate that
there is no element left in the queue.
Queue does not provide blocking queue methods, which will block until the
operation of the method is allowed. BlockingQueue interface defines such
methods.
Known Indirect Subclasses
AbstractQueue<E> |
An abstract class which gives out skeletal implementations for some methods
in Queue which include add, remove, and element that are based on offer,
poll, and peek except that they throw exception to indicate the occurrence of
some error instead of the return value of false or null. |
ArrayBlockingQueue<E> |
A bounded blocking queue backed by an
array. |
BlockingQueue<E> |
A Queue that additionally supports operations
that wait for the queue to become non-empty when retrieving an element,
and wait for space to become available in the queue when storing an
element. |
ConcurrentLinkedQueue<E> |
An unbounded thread-safe queue based on linked nodes. |
DelayQueue<E extends Delayed> |
An unbounded blocking queue of Delayed
elements, in which an element can only be taken when its delay has expired. |
LinkedBlockingQueue<E> |
An optionally-bounded blocking queue based on
linked nodes. |
LinkedList<E> |
LinkedList is an implementation of List, backed by a linked list. |
PriorityBlockingQueue<E> |
An unbounded blocking queue that uses
the same ordering rules as class PriorityQueue and supplies
blocking retrieval operations. |
PriorityQueue<E> |
PriorityQueue holds elements on a priority heap, which orders elements
according to the comparator specified at construction or their natural order. |
SynchronousQueue<E> |
A blocking queue in which each
put must wait for a take, and vice versa. |
Summary
Public Methods
add,
addAll,
clear,
contains,
containsAll,
equals,
hashCode,
isEmpty,
iterator,
remove,
removeAll,
retainAll,
size,
toArray,
toArray
Details
Public Methods
public
E
element()
Gets but not removes the element in the head of the queue. Throws a
NoSuchElementException if there is no element in the queue.
Returns
- the element in the head of the queue.
public
boolean
offer(E o)
Inserts the specified element into the queue provided that the condition
allows such an operation. The method is generally preferable to the
collection.add(E), since the latter might throw an exception if the
operation fails.
Parameters
o
| the specified element to insert into the queue. |
Returns
- true if the operation succeeds and false if it fails.
public
E
peek()
Gets but not removes the element in the head of the queue, or throws
exception if there is no element in the queue.
Returns
- the element in the head of the queue or null if there is no
element in the queue.
public
E
poll()
Gets and removes the element in the head of the queue, or returns null if
there is no element in the queue.
Returns
- the element in the head of the queue or null if there is no
element in the queue.
public
E
remove()
Gets and removes the element in the head of the queue. Throws a
NoSuchElementException if there is no element in the queue.
Returns
- the element in the head of the queue.