Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

A Queue<E> is an ordered Collection of elements of type E. Unlike List, the Queue interface does not permit indexed access to its elements: elements may be inserted at the tail of the queue and may be removed from the head of the queue, but the elements in between may not be accessed by their position. Unlike Set, Queue implementations do not prohibit duplicate elements.

Queues may be manipulated through the methods of the Collection interface, including iteration via the iterator( ) method and the Iterator object it returns. It is more common to manipulate queues through the more specialized methods defined by the Queue interface, however. Place an element at the tail of the queue with offer( ). If the queue is already full, offer( ) returns false. Remove an element from the head of the queue with remove( ) or poll( ). These methods differ only in the case of an empty queue: remove( ) throws an unchecked NoSuchElementException and poll( ) returns null. (Most queue implementations prohibit null elements for this reason, but LinkedList is an exception.) Query the element at the head of a queue without removing it with element( ) or peek( ). If the queue is empty, element( ) throws NoSuchElementException and peek( ) returns null.

Most Queue implementations order their elements in first-in, first-out (FIFO) order. Other implementations may provide other orderings. A queue Iterator is not required to traverse the queue's elements in order. A Queue implementation with a fixed size is a bounded queue. When a bounded queue is full, it is not possible to insert a new element until an element is first removed. Unlike the List and Set interfaces, the Queue interface does not require implementations to override the equals( ) method, and Queue implementations typically do not override it.

In Java 5.0, the LinkedList class has been retrofitted to implement Queue as well as List. PriorityQueue is a Queue implementation that orders elements based on the Comparable or Comparator interfaces. AbstractQueue is an abstract implementation that offers partial support for simple Queue implementations. The java.util.concurrent package defines a BlockingQueue interface that extends this implementation and includes Queue and BlockingQueue implementations that are useful in multithreaded programming.

Figure 16-51. java.util.Queue<E>

public interface Queue<E> extends Collection<E> {
// Public Instance Methods
     E element( );  
     boolean offer(E o);  
     E peek( );  
     E poll( );  
     E remove( );  


You are currently reading a PREVIEW of this book.


Get instant access to over $1 million worth of books and videos.


Start a Free Trial

  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint