java.util
Class PriorityQueue<E>

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by java.util.AbstractCollection sample code for java.util.AbstractCollection definition code for java.util.AbstractCollection <E>
      extended by java.util.AbstractQueue sample code for java.util.AbstractQueue definition code for java.util.AbstractQueue <E>
          extended by java.util.PriorityQueue<E>
Type Parameters:
E - the type of elements held in this collection
All Implemented Interfaces:
Serializable sample code for java.io.Serializable definition code for java.io.Serializable , Iterable sample code for java.lang.Iterable definition code for java.lang.Iterable <E>, Collection sample code for java.util.Collection definition code for java.util.Collection <E>, Queue sample code for java.util.Queue definition code for java.util.Queue <E>

public class PriorityQueue<E>
extends AbstractQueue sample code for java.util.AbstractQueue definition code for java.util.AbstractQueue <E>
implements Serializable sample code for java.io.Serializable definition code for java.io.Serializable

An unbounded priority queue sample code for java.util.Queue definition code for java.util.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 sample code for java.lang.Comparable definition code for java.lang.Comparable ), or according to a Comparator sample code for java.util.Comparator definition code for java.util.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 sample code for java.util.Collection definition code for java.util.Collection and Iterator sample code for java.util.Iterator definition code for java.util.Iterator interfaces. The Iterator provided in method iterator() sample code for java.util.PriorityQueue.iterator() definition code for java.util.PriorityQueue.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 sample code for java.util.concurrent.PriorityBlockingQueue definition code for java.util.concurrent.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 Java Collections Framework.

Since:
1.5
See Also:
Serialized Form

Constructor Summary
PriorityQueue sample code for java.util.PriorityQueue.PriorityQueue() definition code for java.util.PriorityQueue.PriorityQueue() ()
          Creates a PriorityQueue with the default initial capacity (11) that orders its elements according to their natural ordering (using Comparable).
PriorityQueue sample code for java.util.PriorityQueue.PriorityQueue(java.util.Collection) definition code for java.util.PriorityQueue.PriorityQueue(java.util.Collection) (Collection sample code for java.util.Collection definition code for java.util.Collection <? extends E> c)
          Creates a PriorityQueue containing the elements in the specified collection.
PriorityQueue sample code for java.util.PriorityQueue.PriorityQueue(int) definition code for java.util.PriorityQueue.PriorityQueue(int) (int initialCapacity)
          Creates a PriorityQueue with the specified initial capacity that orders its elements according to their natural ordering (using Comparable).
PriorityQueue sample code for java.util.PriorityQueue.PriorityQueue(int, java.util.Comparator) definition code for java.util.PriorityQueue.PriorityQueue(int, java.util.Comparator) (int initialCapacity, Comparator sample code for java.util.Comparator definition code for java.util.Comparator <? super E> comparator)
          Creates a PriorityQueue with the specified initial capacity that orders its elements according to the specified comparator.
PriorityQueue sample code for java.util.PriorityQueue.PriorityQueue(java.util.PriorityQueue) definition code for java.util.PriorityQueue.PriorityQueue(java.util.PriorityQueue) (PriorityQueue sample code for java.util.PriorityQueue definition code for java.util.PriorityQueue <? extends E> c)
          Creates a PriorityQueue containing the elements in the specified collection.
PriorityQueue sample code for java.util.PriorityQueue.PriorityQueue(java.util.SortedSet) definition code for java.util.PriorityQueue.PriorityQueue(java.util.SortedSet) (SortedSet sample code for java.util.SortedSet definition code for java.util.SortedSet <? extends E> c)
          Creates a PriorityQueue containing the elements in the specified collection.
 
Method Summary
 boolean add sample code for java.util.PriorityQueue.add(E) definition code for java.util.PriorityQueue.add(E) (E o)
          Adds the specified element to this queue.
 void clear sample code for java.util.PriorityQueue.clear() definition code for java.util.PriorityQueue.clear() ()
          Removes all elements from the priority queue.
 Comparator sample code for java.util.Comparator definition code for java.util.Comparator <? super E> comparator sample code for java.util.PriorityQueue.comparator() definition code for java.util.PriorityQueue.comparator() ()
          Returns the comparator used to order this collection, or null if this collection is sorted according to its elements natural ordering (using Comparable).
 Iterator sample code for java.util.Iterator definition code for java.util.Iterator <E> iterator sample code for java.util.PriorityQueue.iterator() definition code for java.util.PriorityQueue.iterator() ()
          Returns an iterator over the elements in this queue.
 boolean offer sample code for java.util.PriorityQueue.offer(E) definition code for java.util.PriorityQueue.offer(E) (E o)
          Inserts the specified element into this priority queue.
 E peek sample code for java.util.PriorityQueue.peek() definition code for java.util.PriorityQueue.peek() ()
          Retrieves, but does not remove, the head of this queue, returning null if this queue is empty.
 E poll sample code for java.util.PriorityQueue.poll() definition code for java.util.PriorityQueue.poll() ()
          Retrieves and removes the head of this queue, or null if this queue is empty.
 boolean remove sample code for java.util.PriorityQueue.remove(java.lang.Object) definition code for java.util.PriorityQueue.remove(java.lang.Object) (Object sample code for java.lang.Object definition code for java.lang.Object  o)
          Removes a single instance of the specified element from this queue, if it is present.
 int size sample code for java.util.PriorityQueue.size() definition code for java.util.PriorityQueue.size() ()
          Returns the number of elements in this collection.
 
Methods inherited from class java.util.AbstractQueue sample code for java.util.AbstractQueue definition code for java.util.AbstractQueue
addAll sample code for java.util.AbstractQueue.addAll(java.util.Collection) definition code for java.util.AbstractQueue.addAll(java.util.Collection) , element sample code for java.util.AbstractQueue.element() definition code for java.util.AbstractQueue.element() , remove sample code for java.util.AbstractQueue.remove() definition code for java.util.AbstractQueue.remove()
 
Methods inherited from class java.util.AbstractCollection sample code for java.util.AbstractCollection definition code for java.util.AbstractCollection
contains sample code for java.util.AbstractCollection.contains(java.lang.Object) definition code for java.util.AbstractCollection.contains(java.lang.Object) , containsAll sample code for java.util.AbstractCollection.containsAll(java.util.Collection) definition code for java.util.AbstractCollection.containsAll(java.util.Collection) , isEmpty sample code for java.util.AbstractCollection.isEmpty() definition code for java.util.AbstractCollection.isEmpty() , removeAll sample code for java.util.AbstractCollection.removeAll(java.util.Collection) definition code for java.util.AbstractCollection.removeAll(java.util.Collection) , retainAll sample code for java.util.AbstractCollection.retainAll(java.util.Collection) definition code for java.util.AbstractCollection.retainAll(java.util.Collection) , toArray sample code for java.util.AbstractCollection.toArray() definition code for java.util.AbstractCollection.toArray() , toArray sample code for java.util.AbstractCollection.toArray(T[]) definition code for java.util.AbstractCollection.toArray(T[]) , toString sample code for java.util.AbstractCollection.toString() definition code for java.util.AbstractCollection.toString()
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
clone sample code for java.lang.Object.clone() definition code for java.lang.Object.clone() , equals sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) , finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , hashCode sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 
Methods inherited from interface java.util.Collection sample code for java.util.Collection definition code for java.util.Collection
contains sample code for java.util.Collection.contains(java.lang.Object) definition code for java.util.Collection.contains(java.lang.Object) , containsAll sample code for java.util.Collection.containsAll(java.util.Collection) definition code for java.util.Collection.containsAll(java.util.Collection) , equals sample code for java.util.Collection.equals(java.lang.Object) definition code for java.util.Collection.equals(java.lang.Object) , hashCode sample code for java.util.Collection.hashCode() definition code for java.util.Collection.hashCode() , isEmpty sample code for java.util.Collection.isEmpty() definition code for java.util.Collection.isEmpty() , removeAll sample code for java.util.Collection.removeAll(java.util.Collection) definition code for java.util.Collection.removeAll(java.util.Collection) , retainAll sample code for java.util.Collection.retainAll(java.util.Collection) definition code for java.util.Collection.retainAll(java.util.Collection) , toArray sample code for java.util.Collection.toArray() definition code for java.util.Collection.toArray() , toArray sample code for java.util.Collection.toArray(T[]) definition code for java.util.Collection.toArray(T[])
 

Constructor Detail

PriorityQueue sample code for java.util.PriorityQueue() definition code for java.util.PriorityQueue()

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


PriorityQueue sample code for java.util.PriorityQueue(int) definition code for java.util.PriorityQueue(int)

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 sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - if initialCapacity is less than 1

PriorityQueue sample code for java.util.PriorityQueue(int, java.util.Comparator<? super E>) definition code for java.util.PriorityQueue(int, java.util.Comparator<? super E>)

public PriorityQueue(int initialCapacity,
                     Comparator sample code for java.util.Comparator definition code for java.util.Comparator <? super 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 sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - if initialCapacity is less than 1

PriorityQueue sample code for java.util.PriorityQueue(java.util.Collection<? extends E>) definition code for java.util.PriorityQueue(java.util.Collection<? extends E>)

public PriorityQueue(Collection sample code for java.util.Collection definition code for java.util.Collection <? extends 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 sample code for java.util.SortedSet definition code for java.util.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 sample code for java.lang.ClassCastException definition code for java.lang.ClassCastException - if elements of the specified collection cannot be compared to one another according to the priority queue's ordering.
NullPointerException sample code for java.lang.NullPointerException definition code for java.lang.NullPointerException - if c or any element within it is null

PriorityQueue sample code for java.util.PriorityQueue(java.util.PriorityQueue<? extends E>) definition code for java.util.PriorityQueue(java.util.PriorityQueue<? extends E>)

public PriorityQueue(PriorityQueue sample code for java.util.PriorityQueue definition code for java.util.PriorityQueue <? extends 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 sample code for java.lang.ClassCastException definition code for java.lang.ClassCastException - if elements of the specified collection cannot be compared to one another according to the priority queue's ordering.
NullPointerException sample code for java.lang.NullPointerException definition code for java.lang.NullPointerException - if c or any element within it is null

PriorityQueue sample code for java.util.PriorityQueue(java.util.SortedSet<? extends E>) definition code for java.util.PriorityQueue(java.util.SortedSet<? extends E>)

public PriorityQueue(SortedSet sample code for java.util.SortedSet definition code for java.util.SortedSet <? extends 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 sample code for java.lang.ClassCastException definition code for java.lang.ClassCastException - if elements of the specified collection cannot be compared to one another according to the priority queue's ordering.
NullPointerException sample code for java.lang.NullPointerException definition code for java.lang.NullPointerException - if c or any element within it is null
Method Detail

offer sample code for java.util.PriorityQueue.offer(E) definition code for java.util.PriorityQueue.offer(E)

public boolean offer(E o)
Inserts the specified element into this priority queue.

Specified by:
offer sample code for java.util.Queue.offer(E) definition code for java.util.Queue.offer(E) in interface Queue sample code for java.util.Queue definition code for java.util.Queue <E>
Parameters:
o - the element to insert.
Returns:
true
Throws:
ClassCastException sample code for java.lang.ClassCastException definition code for java.lang.ClassCastException - if the specified element cannot be compared with elements currently in the priority queue according to the priority queue's ordering.
NullPointerException sample code for java.lang.NullPointerException definition code for java.lang.NullPointerException - if the specified element is null.

peek sample code for java.util.PriorityQueue.peek() definition code for java.util.PriorityQueue.peek()

public E peek()
Description copied from interface: Queue sample code for java.util.Queue.peek() definition code for java.util.Queue.peek()
Retrieves, but does not remove, the head of this queue, returning null if this queue is empty.

Specified by:
peek sample code for java.util.Queue.peek() definition code for java.util.Queue.peek() in interface Queue sample code for java.util.Queue definition code for java.util.Queue <E>
Returns:
the head of this queue, or null if this queue is empty.

add sample code for java.util.PriorityQueue.add(E) definition code for java.util.PriorityQueue.add(E)

public boolean add(E o)
Adds the specified element to this queue.

Specified by:
add sample code for java.util.Collection.add(E) definition code for java.util.Collection.add(E) in interface Collection sample code for java.util.Collection definition code for java.util.Collection <E>
Overrides:
add sample code for java.util.AbstractQueue.add(E) definition code for java.util.AbstractQueue.add(E) in class AbstractQueue sample code for java.util.AbstractQueue definition code for java.util.AbstractQueue <E>
Parameters:
o - the element
Returns:
true (as per the general contract of Collection.add).
Throws:
NullPointerException sample code for java.lang.NullPointerException definition code for java.lang.NullPointerException - if the specified element is null.
ClassCastException sample code for java.lang.ClassCastException definition code for java.lang.ClassCastException - if the specified element cannot be compared with elements currently in the priority queue according to the priority queue's ordering.

remove sample code for java.util.PriorityQueue.remove(java.lang.Object) definition code for java.util.PriorityQueue.remove(java.lang.Object)

public boolean remove(Object sample code for java.lang.Object definition code for java.lang.Object  o)
Removes a single instance of the specified element from this queue, if it is present.

Specified by:
remove sample code for java.util.Collection.remove(java.lang.Object) definition code for java.util.Collection.remove(java.lang.Object) in interface Collection sample code for java.util.Collection definition code for java.util.Collection <E>
Overrides:
remove sample code for java.util.AbstractCollection.remove(java.lang.Object) definition code for java.util.AbstractCollection.remove(java.lang.Object) in class AbstractCollection sample code for java.util.AbstractCollection definition code for java.util.AbstractCollection <E>
Parameters:
o - element to be removed from this collection, if present.
Returns:
true if the collection contained the specified element.

iterator sample code for java.util.PriorityQueue.iterator() definition code for java.util.PriorityQueue.iterator()

public Iterator sample code for java.util.Iterator definition code for java.util.Iterator <E> iterator()
Returns an iterator over the elements in this queue. The iterator does not return the elements in any particular order.

Specified by:
iterator sample code for java.lang.Iterable.iterator() definition code for java.lang.Iterable.iterator() in interface Iterable sample code for java.lang.Iterable definition code for java.lang.Iterable <E>
Specified by:
iterator sample code for java.util.Collection.iterator() definition code for java.util.Collection.iterator() in interface Collection sample code for java.util.Collection definition code for java.util.Collection <E>
Specified by:
iterator sample code for java.util.AbstractCollection.iterator() definition code for java.util.AbstractCollection.iterator() in class AbstractCollection sample code for java.util.AbstractCollection definition code for java.util.AbstractCollection <E>
Returns:
an iterator over the elements in this queue.

size sample code for java.util.PriorityQueue.size() definition code for java.util.PriorityQueue.size()

public int size()
Description copied from class: AbstractCollection sample code for java.util.AbstractCollection.size() definition code for java.util.AbstractCollection.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 sample code for java.util.Collection.size() definition code for java.util.Collection.size() in interface Collection sample code for java.util.Collection definition code for java.util.Collection <E>
Specified by:
size sample code for java.util.AbstractCollection.size() definition code for java.util.AbstractCollection.size() in class AbstractCollection sample code for java.util.AbstractCollection definition code for java.util.AbstractCollection <E>
Returns:
the number of elements in this collection.

clear sample code for java.util.PriorityQueue.clear() definition code for java.util.PriorityQueue.clear()

public void clear()
Removes all elements from the priority queue. The queue will be empty after this call returns.

Specified by:
clear sample code for java.util.Collection.clear() definition code for java.util.Collection.clear() in interface Collection sample code for java.util.Collection definition code for java.util.Collection <E>
Overrides:
clear sample code for java.util.AbstractQueue.clear() definition code for java.util.AbstractQueue.clear() in class AbstractQueue sample code for java.util.AbstractQueue definition code for java.util.AbstractQueue <E>

poll sample code for java.util.PriorityQueue.poll() definition code for java.util.PriorityQueue.poll()

public E poll()
Description copied from interface: Queue sample code for java.util.Queue.poll() definition code for java.util.Queue.poll()
Retrieves and removes the head of this queue, or null if this queue is empty.

Specified by:
poll sample code for java.util.Queue.poll() definition code for java.util.Queue.poll() in interface Queue sample code for java.util.Queue definition code for java.util.Queue <E>
Returns:
the head of this queue, or null if this queue is empty.

comparator sample code for java.util.PriorityQueue.comparator() definition code for java.util.PriorityQueue.comparator()

public Comparator sample code for java.util.Comparator definition code for java.util.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).

Returns:
the comparator used to order this collection, or null if this collection is sorted according to its elements natural ordering.