java.util

Class PriorityQueue<E>

Implemented Interfaces:
Collection<E>, Iterable<T>, Queue<E>, Serializable

public class PriorityQueue<E>
extends AbstractQueue<E>
implements Serializable

An unbounded priority queue based on a priority heap. This queue orders elements according to an order specified at construction time, which is specified either according to their natural order (see Comparable), or according to a Comparator, depending on which constructor is used. A priority queue does not permit null elements. A priority queue relying on natural ordering also does not permit insertion of non-comparable objects (doing so may result in ClassCastException).

The head of this queue is the least element with respect to the specified ordering. If multiple elements are tied for least value, the head is one of those elements -- ties are broken arbitrarily. The queue retrieval operations poll, remove, peek, and element access the element at the head of the queue.

A priority queue is unbounded, but has an internal capacity governing the size of an array used to store the elements on the queue. It is always at least as large as the queue size. As elements are added to a priority queue, its capacity grows automatically. The details of the growth policy are not specified.

This class and its iterator implement all of the optional methods of the Collection and Iterator interfaces. The Iterator provided in method iterator() is not guaranteed to traverse the elements of the PriorityQueue in any particular order. If you need ordered traversal, consider using Arrays.sort(pq.toArray()).

Note that this implementation is not synchronized. Multiple threads should not access a PriorityQueue instance concurrently if any of the threads modifies the list structurally. Instead, use the thread-safe PriorityBlockingQueue class.

Implementation note: this implementation provides O(log(n)) time for the insertion methods (offer, poll, remove() and add) methods; linear time for the remove(Object) and contains(Object) methods; and constant time for the retrieval methods (peek, element, and size).

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

Since:
1.5
See Also:
Serialized Form

Constructor Summary

PriorityQueue()
Creates a PriorityQueue with the default initial capacity (11) that orders its elements according to their natural ordering (using Comparable).
PriorityQueue(E> c)
Creates a PriorityQueue containing the elements in the specified collection.
PriorityQueue(E> c)
Creates a PriorityQueue containing the elements in the specified collection.
PriorityQueue(E> c)
Creates a PriorityQueue containing the elements in the specified collection.
PriorityQueue(int initialCapacity)
Creates a PriorityQueue with the specified initial capacity that orders its elements according to their natural ordering (using Comparable).
PriorityQueue(int initialCapacity, E> comparator)
Creates a PriorityQueue with the specified initial capacity that orders its elements according to the specified comparator.

Method Summary

boolean
add(E o)
Adds the specified element to this queue.
void
clear()
Removes all elements from the priority queue.
Iterator
iterator()
Returns an iterator over the elements in this queue.
boolean
offer(E o)
Inserts the specified element into this priority queue.
E
peek()
E
poll()
boolean
remove(Object o)
Removes a single instance of the specified element from this queue, if it is present.
int
size()
Returns the number of elements in this collection.
Comparator
super E> comparator()
Returns the comparator used to order this collection, or null if this collection is sorted according to its elements natural ordering (using Comparable).

Methods inherited from class java.util.AbstractQueue<E>

add, addAll, clear, element, remove

Methods inherited from class java.util.AbstractCollection<E>

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

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Details

PriorityQueue

public PriorityQueue()
Creates a PriorityQueue with the default initial capacity (11) that orders its elements according to their natural ordering (using Comparable).
Usages and Demos :

View More Examples of PriorityQueue()
   1:         assertEquals(1, queue.size());
   2:         queue = new PriorityQueue();
   3:         assertTrue(queue.offer("b"));
   4:         assertEquals(1, queue.size());

View Full Code Here
   1: {
   2:   public PriorityQueue()
   3:   {
   4:     super();
   5:   }

View Full Code Here
   1:         queue.peek();
   2:         Object priorityQueue = new PriorityQueue();
   3:         queue = (Queue) priorityQueue;
   4:         ...
   5:     public void testInstanceOf() throws Exception {
   6:         assertTrue(new PriorityQueue() instanceof Queue);
   7:         assertTrue(new LinkedList() instanceof Queue);
   8:         assertFalse(new Object() instanceof Queue);
   9:         assertTrue(new Integer(1) instanceof Number);

View Full Code Here
   1: 
   2:   public PriorityQueue() {
   3:     this(DEFAULT_NUM_PRIORITIES);
   4:   }

View Full Code Here
   1: 
   2:     public PriorityQueue() {
   3:         this(DEFAULT_INITIAL_CAPACITY, null);
   4:     }

View Full Code Here

PriorityQueue

public PriorityQueue(E> c)
Creates a PriorityQueue containing the elements in the specified collection. The priority queue has an initial capacity of 110% of the size of the specified collection or 1 if the collection is empty. If the specified collection is an instance of a SortedSet or is another PriorityQueue, the priority queue will be sorted according to the same comparator, or according to its elements' natural order if the collection is sorted according to its elements' natural order. Otherwise, the priority queue is ordered according to its elements' natural order.
Parameters:
c - the collection whose elements are to be placed into this priority queue.
Throws:
ClassCastException - if elements of the specified collection cannot be compared to one another according to the priority queue's ordering.
NullPointerException - if c or any element within it is null
Usages and Demos :

View More Examples of PriorityQueue(E> c)
   1:     List list = Arrays.asList(args);
   2:     PriorityQueue queue = new PriorityQueue(list);
   3:     System.out.println(queue);
   4:         ...
   5:     queue = new PriorityQueue(5);
   6:     try {
   7:       System.out.println(queue.removeFirst());
   8:     } catch (NoSuchElementException e) {

View Full Code Here
   1:         Arrays.fill(capacities, PONGS_PER_HOP);
   2:         pongQueue = new PriorityQueue( capacities );
   3:     }
   4:     
   5:     public static PongCache getInstance()

View Full Code Here
   1:     private PriorityQueue populatedQueue(int n) {
   2:         PriorityQueue q = new PriorityQueue(n);
   3:         assertTrue(q.isEmpty());
   4:         ...
   5:     public void testConstructor1() {
   6:         assertEquals(0, new PriorityQueue(SIZE).size());
   7:     }
   8:         ...
   9:         try {
  10:             PriorityQueue q = new PriorityQueue(0);
  11:             shouldThrow();

View Full Code Here

PriorityQueue

public PriorityQueue(E> c)
Creates a PriorityQueue containing the elements in the specified collection. The priority queue has an initial capacity of 110% of the size of the specified collection or 1 if the collection is empty. This priority queue will be sorted according to the same comparator as the given collection, or according to its elements' natural order if the collection is sorted according to its elements' natural order.
Parameters:
c - the collection whose elements are to be placed into this priority queue.
Throws:
ClassCastException - if elements of the specified collection cannot be compared to one another according to the priority queue's ordering.
NullPointerException - if c or any element within it is null
Usages and Demos :

View More Examples of PriorityQueue(E> c)
   1:     List list = Arrays.asList(args);
   2:     PriorityQueue queue = new PriorityQueue(list);
   3:     System.out.println(queue);
   4:         ...
   5:     queue = new PriorityQueue(5);
   6:     try {
   7:       System.out.println(queue.removeFirst());
   8:     } catch (NoSuchElementException e) {

View Full Code Here
   1:         Arrays.fill(capacities, PONGS_PER_HOP);
   2:         pongQueue = new PriorityQueue( capacities );
   3:     }
   4:     
   5:     public static PongCache getInstance()

View Full Code Here
   1:     private PriorityQueue populatedQueue(int n) {
   2:         PriorityQueue q = new PriorityQueue(n);
   3:         assertTrue(q.isEmpty());
   4:         ...
   5:     public void testConstructor1() {
   6:         assertEquals(0, new PriorityQueue(SIZE).size());
   7:     }
   8:         ...
   9:         try {
  10:             PriorityQueue q = new PriorityQueue(0);
  11:             shouldThrow();

View Full Code Here

PriorityQueue

public PriorityQueue(E> c)
Creates a PriorityQueue containing the elements in the specified collection. The priority queue has an initial capacity of 110% of the size of the specified collection or 1 if the collection is empty. This priority queue will be sorted according to the same comparator as the given collection, or according to its elements' natural order if the collection is sorted according to its elements' natural order.
Parameters:
c - the collection whose elements are to be placed into this priority queue.
Throws:
ClassCastException - if elements of the specified collection cannot be compared to one another according to the priority queue's ordering.
NullPointerException - if c or any element within it is null
Usages and Demos :

View More Examples of PriorityQueue(E> c)
   1:     List list = Arrays.asList(args);
   2:     PriorityQueue queue = new PriorityQueue(list);
   3:     System.out.println(queue);
   4:         ...
   5:     queue = new PriorityQueue(5);
   6:     try {
   7:       System.out.println(queue.removeFirst());
   8:     } catch (NoSuchElementException e) {

View Full Code Here
   1:         Arrays.fill(capacities, PONGS_PER_HOP);
   2:         pongQueue = new PriorityQueue( capacities );
   3:     }
   4:     
   5:     public static PongCache getInstance()

View Full Code Here
   1:     private PriorityQueue populatedQueue(int n) {
   2:         PriorityQueue q = new PriorityQueue(n);
   3:         assertTrue(q.isEmpty());
   4:         ...
   5:     public void testConstructor1() {
   6:         assertEquals(0, new PriorityQueue(SIZE).size());
   7:     }
   8:         ...
   9:         try {
  10:             PriorityQueue q = new PriorityQueue(0);
  11:             shouldThrow();

View Full Code Here

PriorityQueue

public PriorityQueue(int initialCapacity)
Creates a PriorityQueue with the specified initial capacity that orders its elements according to their natural ordering (using Comparable).
Parameters:
initialCapacity - the initial capacity for this priority queue.
Throws:
IllegalArgumentException - if initialCapacity is less than 1
Usages and Demos :

View More Examples of PriorityQueue(int initialCapacity)
   1:     List list = Arrays.asList(args);
   2:     PriorityQueue queue = new PriorityQueue(list);
   3:     System.out.println(queue);
   4:         ...
   5:     queue = new PriorityQueue(5);
   6:     try {
   7:       System.out.println(queue.removeFirst());
   8:     } catch (NoSuchElementException e) {

View Full Code Here
   1:         Arrays.fill(capacities, PONGS_PER_HOP);
   2:         pongQueue = new PriorityQueue( capacities );
   3:     }
   4:     
   5:     public static PongCache getInstance()

View Full Code Here
   1:     private PriorityQueue populatedQueue(int n) {
   2:         PriorityQueue q = new PriorityQueue(n);
   3:         assertTrue(q.isEmpty());
   4:         ...
   5:     public void testConstructor1() {
   6:         assertEquals(0, new PriorityQueue(SIZE).size());
   7:     }
   8:         ...
   9:         try {
  10:             PriorityQueue q = new PriorityQueue(0);
  11:             shouldThrow();

View Full Code Here

PriorityQueue

public PriorityQueue(int initialCapacity,
                     E> comparator)
Creates a PriorityQueue with the specified initial capacity that orders its elements according to the specified comparator.
Parameters:
initialCapacity - the initial capacity for this priority queue.
comparator - the comparator used to order this priority queue. If null then the order depends on the elements' natural ordering.
Throws:
IllegalArgumentException - if initialCapacity is less than 1
Usages and Demos :

View More Examples of PriorityQueue(int initialCapacity,E> comparator)
   1:             MyReverseComparator cmp = new MyReverseComparator();
   2:             PriorityQueue q = new PriorityQueue(SIZE, cmp);
   3:             assertEquals(cmp, q.comparator());
   4:             Integer[] ints = new Integer[SIZE];
   5:             for (int i = 0; i < SIZE; ++i)

View Full Code Here

Method Details

add

public boolean add(E o)
Adds the specified element to this queue.
Specified by:
add in interface Collection<E>
Overrides:
add in interface AbstractQueue<E>
Returns:
true (as per the general contract of Collection.add).
Throws:
NullPointerException - if the specified element is null.
ClassCastException - if the specified element cannot be compared with elements currently in the priority queue according to the priority queue's ordering.
Usages and Demos :

View More Examples of add(E o)
   1: 
   2:     private PriorityQueue populatedQueue(int n) {
   3:         ...
   4:         PriorityQueue q = new PriorityQueue(n);
   5:         assertTrue(q.isEmpty());
   6:         ...
   7:             PriorityQueue q = new PriorityQueue(1);
   8:             q.add(null);
   9:             shouldThrow();

View Full Code Here
   1:             
   2:             r.incommingStates.add(this);
   3:         }
   4:         ...
   5:     protected state[] states;
   6:     protected PriorityQueue changeQueue;
   7:     
   8:         ...
   9:                 else
  10:                     changeQueue.add(s);
  11:             }

View Full Code Here

clear

public void clear()
Removes all elements from the priority queue. The queue will be empty after this call returns.
Specified by:
clear in interface Collection<E>
Overrides:
clear in interface AbstractQueue<E>
Usages and Demos :

View More Examples of clear()
   1: 
   2:     private PriorityQueue populatedQueue(int n) {
   3:         ...
   4:         PriorityQueue q = new PriorityQueue(n);
   5:         assertTrue(q.isEmpty());
   6:         ...
   7:         PriorityQueue q = populatedQueue(SIZE);
   8:         q.clear();
   9:         assertTrue(q.isEmpty());
  10:         ...
  11:         assertFalse(q.isEmpty());
  12:         q.clear();

View Full Code Here
   1:         
   2:     private phex.utils.PriorityQueue caughtHosts;
   3:     private HashSet uniqueCaughtHosts;
   4:         ...
   5:             - capacities[HIGH_PRIORITY] - capacities[NORMAL_PRIORITY];
   6:         caughtHosts = new phex.utils.PriorityQueue( capacities );
   7: 
   8:         ...
   9:     {
  10:         caughtHosts.clear();
  11:         ...
  12:         catchedHostCache.clear();

View Full Code Here

iterator

public Iterator iterator()
Returns an iterator over the elements in this queue. The iterator does not return the elements in any particular order.
Specified by:
iterator in interface Collection<E>
iterator in interface Iterable<T>
Overrides:
iterator in interface AbstractCollection<E>
Returns:
an iterator over the elements in this queue.

offer

public boolean offer(E o)
Inserts the specified element into this priority queue.
Specified by:
offer in interface Queue<E>
Returns:
true
Throws:
ClassCastException - if the specified element cannot be compared with elements currently in the priority queue according to the priority queue's ordering.
NullPointerException - if the specified element is null.
Usages and Demos :

View More Examples of offer(E o)
   1: 
   2:     private PriorityQueue populatedQueue(int n) {
   3:         ...
   4:         PriorityQueue q = new PriorityQueue(n);
   5:         assertTrue(q.isEmpty());
   6:         ...
   7:             PriorityQueue q = new PriorityQueue(1);
   8:             q.offer(null);
   9:             shouldThrow();
  10:         ...
  11:         PriorityQueue q = new PriorityQueue(1);
  12:         assertTrue(q.offer(zero));

View Full Code Here

peek

public E peek()
Specified by:
peek in interface Queue<E>
Usages and Demos :

View More Examples of peek()
   1: 
   2: import edu.emory.mathcs.backport.java.util.PriorityQueue;
   3: 
   4:         ...
   5: {
   6:     private final PriorityQueue heap_;
   7: 
   8:         ...
   9: 
  10:         heap_ = new PriorityQueue(capacity, comparator);
  11:     }
  12:         ...
  13: 
  14:         while ((heap_.peek()) != null && (_result.size() < max))

View Full Code Here

poll

public E poll()
Specified by:
poll in interface Queue<E>
Usages and Demos :

View More Examples of poll()
   1: 
   2:     private PriorityQueue populatedQueue(int n) {
   3:         ...
   4:         PriorityQueue q = new PriorityQueue(n);
   5:         assertTrue(q.isEmpty());
   6:         ...
   7:             for (int i = 0; i < SIZE; ++i)
   8:                 assertEquals(ints[i], q.poll());
   9:         }
  10:         ...
  11:             for (int i = SIZE-1; i >= 0; --i)
  12:                 assertEquals(ints[i], q.poll());

View Full Code Here

remove

public boolean remove(Object o)
Removes a single instance of the specified element from this queue, if it is present.
Specified by:
remove in interface Collection<E>
Overrides:
remove in interface AbstractCollection<E>
Usages and Demos :

View More Examples of remove(Object o)
   1: 
   2:   protected Scheduler(PriorityQueue commandQueue,
   3:                       long queueProcessingInterval,
   4:         ...
   5:             if (id != null) {
   6:               commandQueue.remove(cmd.getId());
   7:               commandQueue.saveToBackup();
   8:         ...
   9:     else {
  10:       recentUsedServers.remove(sgaName);
  11:       return true;

View Full Code Here

size

public int size()
Returns the number of elements in this collection. If the collection contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.
Specified by:
size in interface Collection<E>
Overrides:
size in interface AbstractCollection<E>
Returns:
the number of elements in this collection.
Usages and Demos :

View More Examples of size()
   1:     List list = Arrays.asList(args);
   2:     PriorityQueue queue = new PriorityQueue(list);
   3:     System.out.println(queue);
   4:         ...
   5:     queue = new PriorityQueue(5);
   6:     try {
   7:         ...
   8:     System.out.println(queue);
   9:     while (queue.size() != 0) {
  10:       System.out.println(queue.removeFirst());

View Full Code Here

super E> comparator

public Comparatorsuper E> comparator()
Returns the comparator used to order this collection, or null if this collection is sorted according to its elements natural ordering (using Comparable).
Returns:
the comparator used to order this collection, or null if this collection is sorted according to its elements natural ordering.