Learning Resources
 

The Queue Interface

The Queue Interface

A Queue is a collection for holding elements prior to processing. Besides basic Collectionoperations, queues provide additional insertion, removal, and inspection operations. The Queueinterface follows.

public interface Queue extends Collection {
    E element();
    boolean offer(E e);
    E peek();
    E poll();
    E remove();
}

Each Queuemethod exists in two forms: (1) one throws an exception if the operation fails, and (2) the other returns a special value if the operation fails (either nullor false, depending on the operation). The regular structure of the interface is illustrated in the following table.

Queue Interface Structure
Type of Operation Throws exception Returns special value
Insert add(e) offer(e)
Remove remove() poll()
Examine element() peek()

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 their values — see the Object Ordering section for details). Whatever ordering is used, the head of the queue is the element that would be removed by a call to removeor 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 Queueimplementation must specify its ordering properties.

It is possible for a Queueimplementation to restrict the number of elements that it holds; such queues are known as bounded. Some Queueimplementations in java.util.concurrentare bounded, but the implementations in java.utilare not.

The addmethod, which Queueinherits from Collection, inserts an element unless it would violate the queue's capacity restrictions, in which case it throws IllegalStateException. The offermethod, which is intended solely for use on bounded queues, differs from addonly in that it indicates failure to insert an element by returning false.

The removeand pollmethods both remove and return the head of the queue. Exactly which element gets removed is a function of the queue's ordering policy. The removeand pollmethods differ in their behavior only when the queue is empty. Under these circumstances, removethrows NoSuchElementException, while pollreturns null.

The elementand peekmethods return, but do not remove, the head of the queue. They differ from one another in precisely the same fashion as removeand poll: If the queue is empty, elementthrows NoSuchElementException, while peekreturns null.

Queueimplementations generally do not allow insertion of nullelements. The LinkedListimplementation, which was retrofitted to implement Queue, is an exception. For historical reasons, it permits nullelements, but you should refrain from taking advantage of this, because nullis used as a special return value by the polland peekmethods.

Queue implementations generally do not define element-based versions of the equalsand hashCodemethods but instead inherit the identity-based versions from Object.

The Queueinterface 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 interface java.util.concurrent.BlockingQueue, which extends Queue.

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.

import java.util.*;

public class Countdown {
    public static void main(String[] args) throws InterruptedException {
        int time = Integer.parseInt(args[0]);
        Queue queue = new LinkedList();

        for (int i = time; i >= 0; i--)
            queue.add(i);

        while (!queue.isEmpty()) {
            System.out.println(queue.remove());
            Thread.sleep(1000);
        }
    }
}

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 the sortmethod provided in Collections, but it illustrates the behavior of priority queues.

static  List heapSort(Collection c) {
    Queue queue = new PriorityQueue(c);
    List result = new ArrayList();

    while (!queue.isEmpty())
        result.add(queue.remove());

    return result;
}
--Oracle