java.util

Interface Queue<E>

All Superinterfaces:
Collection<E>, Iterable<T>
Known Subinterfaces:
BlockingQueue<E>
Known Implementing Classes:
AbstractQueue<E>, ArrayBlockingQueue<E>, BucketingService.CollectedList, ConcurrentLinkedQueue<E>, DelayQueue<E,extends,Delayed>, LinkedBlockingQueue<E>, LinkedList<E>, PriorityBlockingQueue<E>, PriorityQueue<E>, SynchronousQueue<E>

public interface Queue<E>
extends Collection<E>

A collection designed for holding elements prior to processing. Besides basic Collection operations, queues provide additional insertion, extraction, and inspection operations.

Queues typically, but do not necessarily, order elements in a FIFO (first-in-first-out) manner. Among the exceptions are priority queues, which order elements according to a supplied comparator, or the elements' natural ordering, and LIFO queues (or stacks) which order the elements LIFO (last-in-first-out). Whatever the ordering used, the head of the queue is that element which would be removed by a call to remove() or poll(). In a FIFO queue, all new elements are inserted at the tail of the queue. Other kinds of queues may use different placement rules. Every Queue implementation must specify its ordering properties.

The offer method inserts an element if possible, otherwise returning false. This differs from the Collection.add method, which can fail to add an element only by throwing an unchecked exception. The offer method is designed for use when failure is a normal, rather than exceptional occurrence, for example, in fixed-capacity (or "bounded") queues.

The remove() and poll() methods remove and return the head of the queue. Exactly which element is removed from the queue is a function of the queue's ordering policy, which differs from implementation to implementation. The remove() and poll() methods differ only in their behavior when 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 head of the queue.

The Queue interface does not define the blocking queue methods, which are common in concurrent programming. These methods, which wait for elements to appear or for space to become available, are defined in the BlockingQueue interface, which extends this interface.

Queue implementations generally do not allow insertion of null elements, although some implementations, such as LinkedList, do not prohibit insertion of null. Even in the implementations that permit it, null should not be inserted into a Queue, as null is also used as a special return value by the poll method to indicate that the queue contains no elements.

Queue implementations generally do not define element-based versions of methods equals and hashCode but instead inherit the identity based versions from class Object, because element-based equality is not always well-defined for queues with the same elements but different ordering properties.

This interface is a member of the ../../../guide/collections/index.html"> Java Collections Framework.

Since:
1.5
See Also:
Collection, LinkedList, PriorityQueue, LinkedBlockingQueue, BlockingQueue, ArrayBlockingQueue, LinkedBlockingQueue, PriorityBlockingQueue

Method Summary

E
element()
Retrieves, but does not remove, the head of this queue.
boolean
offer(E o)
Inserts the specified element into this queue, if possible.
E
peek()
Retrieves, but does not remove, the head of this queue, returning null if this queue is empty.
E
poll()
Retrieves and removes the head of this queue, or null if this queue is empty.
E
remove()
Retrieves and removes the head of this queue.

Methods inherited from interface java.util.Collection<E>

T[] toArray, add, addAll, clear, contains, containsAll, equals, hashCode, isEmpty, iterator, remove, removeAll, retainAll, size, toArray

Methods inherited from interface java.lang.Iterable<T>

iterator

Method Details

element

public E element()
Retrieves, but does not remove, the head of this queue. This method differs from the peek method only in that it throws an exception if this queue is empty.
Returns:
the head of this queue.
Throws:
NoSuchElementException - if this queue is empty.
Usages and Demos :

View More Examples of element()
   1: import java.util.LinkedList;
   2: import java.util.Queue;
   3: 
   4:         ...
   5: 
   6:     public static Object element(Queue queue) {
   7:         if (queue instanceof LinkedList) {
   8:         ...
   9:         }
  10:         return queue.element();
  11:     }

View Full Code Here

offer

public boolean offer(E o)
Inserts the specified element into this queue, if possible. When using queues that may impose insertion restrictions (for example capacity bounds), method offer is generally preferable to method Collection.add(E), which can fail to insert an element only by throwing an exception.
Parameters:
o - the element to insert.
Returns:
true if it was possible to add the element to this queue, else false
Usages and Demos :

View More Examples of offer(E o)
   1: import java.util.LinkedList;
   2: import java.util.Queue;
   3: 
   4:         ...
   5: 
   6:     public static Object element(Queue queue) {
   7:         if (queue instanceof LinkedList) {
   8:         ...
   9:         if (queue instanceof LinkedList) {
  10:             return _LinkedList.offer((LinkedList) queue, o);
  11:         }
  12:         ...
  13:         return queue.offer(o);

View Full Code Here

peek

public E peek()
Retrieves, but does not remove, the head of this queue, returning null if this queue is empty.
Returns:
the head of this queue, or null if this queue is empty.
Usages and Demos :

View More Examples of peek()
   1: import java.util.LinkedList;
   2: import java.util.Queue;
   3: 
   4:         ...
   5: 
   6:     public static Object element(Queue queue) {
   7:         if (queue instanceof LinkedList) {
   8:         ...
   9:         }
  10:         return queue.peek();
  11:     }

View Full Code Here
   1: import java.util.PriorityQueue;
   2: import java.util.Queue;
   3: 
   4:         ...
   5:         }
   6:         Queue queue;
   7:         Object linkedList = new LinkedList();
   8:         ...
   9:         queue = (Queue) linkedList;
  10:         queue.peek();
  11:         Object priorityQueue = new PriorityQueue();
  12:         ...
  13:         }
  14:         queue.peek();

View Full Code Here

poll

public E poll()
Retrieves and removes the head of this queue, or null if this queue is empty.
Returns:
the head of this queue, or null if this queue is empty.
Usages and Demos :

View More Examples of poll()
   1: import java.util.LinkedList;
   2: import java.util.Queue;
   3: 
   4:         ...
   5: 
   6:   public Queue q;
   7: 
   8:         ...
   9:     Object o;
  10:     while ((o = q.poll()) != null) {
  11:       out.println(o);

View Full Code Here
   1: import java.util.LinkedList;
   2: import java.util.Queue;
   3: 
   4:         ...
   5: 
   6:     public static Object element(Queue queue) {
   7:         if (queue instanceof LinkedList) {
   8:         ...
   9:         }
  10:         return queue.poll();
  11:     }

View Full Code Here
   1: import java.util.Iterator;
   2: import java.util.Queue;
   3: import java.util.Set;
   4:         ...
   5: 
   6:     private transient Queue[] selectorQueues;
   7: 
   8:         ...
   9: 
  10:                         Queue[] queues;
  11:                         int count = getSelectorCount();
  12:         ...
  13:                         SelectableChannel channel;
  14:                         while ((channel = (SelectableChannel) queue.poll()) != null)

View Full Code Here

remove

public E remove()
Retrieves and removes the head of this queue. This method differs from the poll method in that it throws an exception if this queue is empty.
Returns:
the head of this queue.
Throws:
NoSuchElementException - if this queue is empty.
Usages and Demos :

View More Examples of remove()
   1:   {
   2:     Queue q1 = new Queue();
   3:         ...
   4:     Queue q2 = new Queue();
   5:     q1.add(new Integer(1));
   6:         ...
   7:     q1.add(new Integer(4));
   8:     q1.remove();
   9:     q1.add(new Integer(5));
  10:         ...
  11:     while (!q1.isEmpty()) {
  12:       Integer i = (Integer)q1.remove();

View Full Code Here
   1: import edu.emory.mathcs.backport.java.util.Collections;
   2: import edu.emory.mathcs.backport.java.util.Queue;
   3: 
   4:         ...
   5: 
   6:         Queue pq = new PriorityQueue(n, new MyComparator());
   7:         for (Iterator i = shuffled.iterator(); i.hasNext(); )
   8:         ...
   9:         while (!pq.isEmpty())
  10:             recons.add(pq.remove());
  11:         if (!recons.equals(sorted))
  12:         ...
  13:         while (!pq.isEmpty())
  14:             recons.add(pq.remove());

View Full Code Here
   1: import java.util.LinkedList;
   2: import java.util.Queue;
   3: 
   4:         ...
   5: 
   6:     public static Object element(Queue queue) {
   7:         if (queue instanceof LinkedList) {
   8:         ...
   9:         }
  10:         return queue.remove();
  11:     }

View Full Code Here
   1: 
   2: public class Queue
   3: {
   4:         ...
   5: 
   6:   public Queue()
   7:   {
   8:         ...
   9:         }
  10:         throw new NoSuchElementException("Queue Enumeration");
  11:       }
  12:         ...
  13:     q1.add(new Integer(4));
  14:     q1.remove();

View Full Code Here