This documentation differs from the official API. Jadeite adds extra features to the API including: variable font sizes, constructions examples, placeholders for classes and methods, and auto-generated “See Also” links. Additionally it is missing some items found in standard Javadoc documentation, including: generics type information, “Deprecated” tags and comments, “See Also” links, along with other minor differences. Please send any questions or feedback to bam@cs.cmu.edu.


java.util
class PriorityQueue

java.lang.Object extended by java.util.AbstractCollection extended by java.util.AbstractQueue extended by java.util.PriorityQueue
All Implemented Interfaces:
Serializable, Collection, Queue

Most common way to construct:

PriorityQueue priorityQueue1 = new PriorityQueue();

Based on 7 examples


public class PriorityQueue
extends AbstractQueue
implements Serializable

An unbounded priority {@linkplain Queue queue} based on a priority heap. The elements of the priority queue are ordered according to their {@linkplain Comparable natural ordering}, or by a {@link Comparator} provided at queue construction time, depending on which constructor is used. A priority queue does not permit {@code null} elements. A priority queue relying on natural ordering also does not permit insertion of non-comparable objects (doing so may result in {@code 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 {@code poll}, {@code remove}, {@code peek}, and {@code 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 {@link Collection} and {@link Iterator} interfaces. The Iterator provided in method {@link #iterator()} is not guaranteed to traverse the elements of the priority queue in any particular order. If you need ordered traversal, consider using {@code Arrays.sort(pq.toArray())}.

Note that this implementation is not synchronized. Multiple threads should not access a {@code PriorityQueue} instance concurrently if any of the threads modifies the queue. Instead, use the thread-safe {@link java.util.concurrent.PriorityBlockingQueue} class.

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

This class is a member of the Java Collections Framework.


Constructor Summary

          Creates a with the default initial capacity (11) that orders its elements according to their java.lang.Comparable.

          Creates a containing the elements in the specified collection.
PriorityQueue(int initialCapacity)

          Creates a with the specified initial capacity that orders its elements according to their java.lang.Comparable.
PriorityQueue(int initialCapacity, Comparator comparator)

          Creates a with the specified initial capacity that orders its elements according to the specified comparator.

          Creates a containing the elements in the specified priority queue.

          Creates a containing the elements in the specified sorted set.
 
Method Summary
 boolean

          Inserts the specified element into this priority queue.
 void

          Removes all of the elements from this priority queue.
 Comparator

          Returns the comparator used to order the elements in this queue, or if this queue is sorted according to the java.lang.Comparable of its elements.
 boolean

          Returns if this queue contains the specified element.
 Iterator

          Returns an iterator over the elements in this queue.
 boolean

          Inserts the specified element into this priority queue.
 Object

          
 Object

          
 boolean

          Removes a single instance of the specified element from this queue, if it is present.
 int

          
 Object[]

          Returns an array containing all of the elements in this queue.
 Object[]

          Returns an array containing all of the elements in this queue; the runtime type of the returned array is that of the specified array.
 
Methods inherited from class java.util.AbstractQueue
add, addAll, clear, element, remove
 
Methods inherited from class java.util.AbstractCollection
add, addAll, clear, contains, containsAll, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PriorityQueue

public PriorityQueue()
Creates a {@code PriorityQueue} with the default initial capacity (11) that orders its elements according to their {@linkplain Comparable natural ordering}.


PriorityQueue

public PriorityQueue(Collection c)
Creates a {@code PriorityQueue} containing the elements in the specified collection. If the specified collection is an instance of a {@link SortedSet} or is another {@code PriorityQueue}, this priority queue will be ordered according to the same ordering. Otherwise, this priority queue will be ordered according to the {@linkplain Comparable natural ordering} of its elements.

Parameters:
c - the collection whose elements are to be placed into this priority queue

PriorityQueue

public PriorityQueue(int initialCapacity)
Creates a {@code PriorityQueue} with the specified initial capacity that orders its elements according to their {@linkplain Comparable natural ordering}.

Parameters:
initialCapacity - the initial capacity for this priority queue

PriorityQueue

public PriorityQueue(int initialCapacity,
                     Comparator comparator)
Creates a {@code 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 that will be used to order this priority queue. If {@code null}, the {@linkplain Comparable natural ordering} of the elements will be used.

PriorityQueue

public PriorityQueue(PriorityQueue c)
Creates a {@code PriorityQueue} containing the elements in the specified priority queue. This priority queue will be ordered according to the same ordering as the given priority queue.

Parameters:
c - the priority queue whose elements are to be placed into this priority queue

PriorityQueue

public PriorityQueue(SortedSet c)
Creates a {@code PriorityQueue} containing the elements in the specified sorted set. This priority queue will be ordered according to the same ordering as the given sorted set.

Parameters:
c - the sorted set whose elements are to be placed into this priority queue
Method Detail

add

public boolean add(Object e)
Inserts the specified element into this priority queue.

Overrides:
add in class AbstractQueue
Parameters:
e
Returns:
{@code true} (as specified by {@link Collection#add})

clear

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

Overrides:
clear in class AbstractQueue

comparator

public Comparator comparator()
Returns the comparator used to order the elements in this queue, or {@code null} if this queue is sorted according to the {@linkplain Comparable natural ordering} of its elements.

Returns:
the comparator used to order this queue, or {@code null} if this queue is sorted according to the natural ordering of its elements

contains

public boolean contains(Object o)
Returns {@code true} if this queue contains the specified element. More formally, returns {@code true} if and only if this queue contains at least one element {@code e} such that {@code o.equals(e)}.

Overrides:
contains in class AbstractCollection
Parameters:
o - object to be checked for containment in this queue
Returns:
{@code true} if this queue contains the specified element

iterator

public Iterator iterator()
Returns an iterator over the elements in this queue. The iterator does not return the elements in any particular order.

Overrides:
iterator in class AbstractCollection
Returns:
an iterator over the elements in this queue

offer

public boolean offer(Object e)
Inserts the specified element into this priority queue.

Parameters:
e
Returns:
{@code true} (as specified by {@link Queue#offer})

peek

public Object peek()

poll

public Object poll()

remove

public boolean remove(Object o)
Removes a single instance of the specified element from this queue, if it is present. More formally, removes an element {@code e} such that {@code o.equals(e)}, if this queue contains one or more such elements. Returns {@code true} if and only if this queue contained the specified element (or equivalently, if this queue changed as a result of the call).

Overrides:
remove in class AbstractCollection
Parameters:
o - element to be removed from this queue, if present
Returns:
{@code true} if this queue changed as a result of the call

size

public int size()
Overrides:
size in class AbstractCollection

toArray

public Object[] toArray()
Returns an array containing all of the elements in this queue. The elements are in no particular order.

The returned array will be "safe" in that no references to it are maintained by this queue. (In other words, this method must allocate a new array). The caller is thus free to modify the returned array.

This method acts as bridge between array-based and collection-based APIs.

Overrides:
toArray in class AbstractCollection
Returns:
an array containing all of the elements in this queue

toArray

public Object[] toArray(Object[] a)
Returns an array containing all of the elements in this queue; the runtime type of the returned array is that of the specified array. The returned array elements are in no particular order. If the queue fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this queue.

If the queue fits in the specified array with room to spare (i.e., the array has more elements than the queue), the element in the array immediately following the end of the collection is set to {@code null}.

Like the {@link #toArray()} method, this method acts as bridge between array-based and collection-based APIs. Further, this method allows precise control over the runtime type of the output array, and may, under certain circumstances, be used to save allocation costs.

Suppose x is a queue known to contain only strings. The following code can be used to dump the queue into a newly allocated array of String:

     String[] y = x.toArray(new String[0]);
Note that toArray(new Object[0]) is identical in function to toArray().

Overrides:
toArray in class AbstractCollection
Parameters:
a - the array into which the elements of the queue are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
Returns:
an array containing all of the elements in this queue


This documentation differs from the official API. Jadeite adds extra features to the API including: variable font sizes, constructions examples, placeholders for classes and methods, and auto-generated “See Also” links. Additionally it is missing some items found in standard Javadoc documentation, including: generics type information, “Deprecated” tags and comments, “See Also” links, along with other minor differences. Please send any questions or feedback to bam@cs.cmu.edu.
This page displays the Jadeite version of the documention, which is derived from the offical documentation that contains this copyright notice:
Copyright 2008 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
The official Sun™ documentation can be found here at http://java.sun.com/javase/6/docs/api/.