QueuesHomepage  « Learn Java6 « Queues

In our final lesson on the different collection types within the Collection hierarchy we look at the PriorityQueue<E> concrete implementation of the Queue<E> interface. So what type of collection is a Queue? A Queue is an ordered list of actions that are to be processed and as such does not allow null elements. Typically, a Queue would be in FIFO (first-in, first-out) order although other orders such as LIFO (last-in, first-out), natural-ordering and custom comparator order are also possible.

Queue Hierarchy Diagramgo to top of page Top

The diagram below is a representation of the Queue hierarchy and covers the interfaces and class we will study in this lesson. The diagram has several interfaces and classes missing but should help in visualisation:

Queue hierarchy

Queue Interfaces & Classesgo to top of page Top

The table below gives a description of each interface and class within the above diagram:

Interface/Class Description
Collection<E>The root interface in the Collection hierarchy which the Queue<E> interface extends. There is no direct implementation of the Collection<E> interface within the JDK.
Queue<E>Interface for holding elements prior to processing.
PriorityQueue<E>Unbounded priority queue implementation of the Queue<E> interface that is based on a priority heap that does not permit null elements.

Queue Types and Orderinggo to top of page Top

The table below extrapolates the commented information about ordering from the diagram above into a more readable tabular format:

Collection Type Ordering
PriorityQueue<E>SortedPIPO (priority-in, priority-out)

PriorityQueuesgo to top of page Top

A PriorityQueue is a sorted, ordered implementation of the Queue<E> interface that permits all elements except null. A PriorityQueue queue is ordered in PIPO (priority-in, priority-out) order. This is achieved by natural order or a custom comparator to do the sorting and elements sorted first get the highest priority, in other words the ordering of the elements represents their priority within the queue.

Often when discusiing Queues mention is made of the head and tail of the queue. The head of the Queue can be thought of as the least ordered element and the tail can be thought of as the most ordered element. So for instance via natural order of A, B, C and D : A is the head and D is the tail.

PriorityQueue<E> Method Overviewgo to top of page Top

The table below shows the declarations of all the methods for the PriorityQueue<E> class implemented from the Queue<E> interface:

Method Declaration Description
Used in the example below
public boolean add(E o)Adds the specified element to this PriorityQueue.
public E poll()Returns and removes the first element of this PriorityQueue.
public boolean remove(Object o)Removes a single instance of the specified element from this PriorityQueue if it is present.
public int size()Returns the number of elements in this PriorityQueue, ie. its cardinality.
Not used in the example below
public void clear()Removes all of the elements from this PriorityQueue.
public Comparator<? super E> comparator()Returns the comparator used to order this PriorityQueue, or null if this PriorityQueue is sorted according to natural ordering (using Comparable).
public Iterator<E> iterator()Returns an iterator over the elements in this PriorityQueue.
public boolean offer(E o)Inserts the specified element into this PriorityQueue.
public E peek()Returns the first element of this PriorityQueue.

Usage for the the first four methods is shown in the example below. For more information on the other methods in the PriorityQueue<E> class the following link will take you to the online version of documentation for the JavaTM 2 Platform Standard Edition 5.0 API Specification. Take a look at the documentation for the PriorityQueue<E> class which you can find by scrolling down the lower left pane and clicking on PriorityQueue.

PriorityQueue<E> Examplego to top of page Top

Lets write a simple class that creates an PriorityQueue and uses some of the methods from the PriorityQueue<E> class:

  Simple class to create an PriorityQueue and use several methods from the class 
import java.util.*; // Import the java.util package

public class PriorityQueueClass {

    public static void main (String[] args) {
        Queue<String> pq = new PriorityQueue<String>();
        String s = "hhh";
        System.out.println("priority queue size = " + pq.size());
        System.out.println("priority queue contains: " + pq);
        System.out.println("priority queue size = " + pq.size());
        System.out.println("priority queue contains: " + pq);

Save, compile and run the PriorityQueueClass class in directory   c:\_Collections in the usual way.

Run priority queue class

The above screenshot shows the results of creating and running our PriorityQueueClass class and using several of the methods contained within the PriorityQueueClass<E> class.

Queues Quizgo to top of page Top

Try the quiz below to test your knowledge of this lesson

Question 1 : You can't put null elements into Queues in the API?
- All concrete implementations of Queues in the APIs do now allow null elements.
Question 2 : What type of ordering does a PriorityQueue have?
- A PriorityQueue is ordered and sorted.
Question 3 : What type of Queue is a PriorityQueue?
- A PriorityQueue is a PIPO (priority-in, priority-out) Queue.
Question 4 : A PriorityQueue is always sorted in natural order?
- A PriorityQueue defaults to natural order but can also be sorted using a custom comparator.
Question 5 : Within a PriorityQueue the elements sorted last are processed first?
- Within a PriorityQueue the elements sorted FIRST are processed first.
Status Bar Please select an answer

What's Next?

In the next lesson we look at the Queue<E> interface for holding elements prior to processing as well as investigating the PriorityQueue<E> concrete implementations of the Queue<E> interface.

<<  Lists                    Maps  >>

go to home page Java6 Tutor Homepage go to top of page Top