Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
AQueue
is a collection for holding elements prior to processing. Besides basicCollection
operations, queues provide additional insertion, removal, and inspection operations. TheQueue
interface follows:Eachpublic interface Queue<E> extends Collection<E> { E element(); boolean offer(E o); E peek(); E poll(); E remove(); }Queue
method exists in two forms: one throws an exception if the operation fails, the other returns a special value (eithernull
orfalse
, depending on the operation). The regular structure of the interface is illustrated in the following table:Queues typically, but not necessarily, order elements in a FIFO (first-in-first-out) manner. Among the exceptions are priority queues, which order elements according to a their values (see the section Object Ordering for details). Whatever ordering is used, the head of the queue is the element that would be removed by a call to
Queue Interface Structure Throws exception Returns special value Insert add(e)
offer(e)
Remove remove()
poll()
Examine element()
peek()
remove
orpoll
. In a FIFO queue, all new elements are inserted at the tail of the queue. Other kinds of queues may use different placement rules. EveryQueue
implementation must specify its ordering properties.It is possible for a queue implementation to restrict the number of elements that it holds. Such queues are known as bounded. Some
Queue
implementations injava.util.concurrent
are bounded, but the implementations injava.util
are not.The
add
method, whichQueue
inherits fromCollection
, inserts an element unless it would violate the queue's capacity restrictions, in which case it throwsIllegalStateExcepion
. Theoffer
method, which is intended solely for use on bounded queues, differs from add only in that it indicates failure to insert an element by returningfalse
.The
remove
andpoll
methods both remove and return the head of the queue. Exactly which element gets removed is a function of the queue's ordering policy. Theremove
andpoll
methods differ in their behavior only when the queue is empty. Under these circumstances,remove
throwsNoSuchElementException
, whilepoll
returnsnull
.The
element
andpeek
methods return, but do not remove, the head of the queue. They differ from one another in precisely the same fashion asremove
andpoll
: if the queue is empty,element
throwsNoSuchElementException
whilepeek
returnsfalse
.
Queue
implementations generally do not allow insertion of null elements. TheLinkedList
implementation (which was retrofitted to implementQueue
) is an exception. For historical reasons, it permits null elements, but you should refrain from taking advantage of this, asnull
is used as a special return value by thepoll
andpeek
methods.Queue implementations generally do not define element-based versions of the
equals
andhashCode
methods but instead inherit the identity-based versions fromObject
.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 thejava.util.concurrent.BlockingQueue
interface, which extendsQueue
.In the following example program, a queue is used to implement a countdown timer. The queue is preloaded with all the integer values from a number specified on the command line to zero, in descending order. Then the values are removed from the queue and printed at one second intervals. The program is artificial in that it would be more natural to do the same thing without using a queue, but it illustrates the use of a queue to store elements prior to subsequent processing:
In the following example, a priority queue is used to sort a collection of elements. Again this program is artificial, in that there is no reason to use it in favor of theimport java.util.*; public class Countdown { public static void main(String[] args) throws InterruptedException { int time = Integer.parseInt(args[0]); Queue<Integer> queue = new LinkedList<Integer>(); for (int i = time; i >= 0; i--) queue.add(i); while(!queue.isEmpty()) { System.out.println(queue.remove()); Thread.sleep(1000); } } }sort
method provided inCollections
, but it illustrates the behavior of priority queues:static <E> List<E> heapSort(Collection<E> c) { Queue<E> queue = new PriorityQueue<E>(c); List<E> result = new ArrayList<E>(); while (!queue.isEmpty()) result.add(queue.remove()); return result; }
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.