Skip to content

Most visited

Recently visited

navigation
Added in API level 1

PriorityQueue

public class PriorityQueue
extends AbstractQueue<E> implements Serializable

java.lang.Object
   ↳ java.util.AbstractCollection<E>
     ↳ java.util.AbstractQueue<E>
       ↳ java.util.PriorityQueue<E>


A PriorityQueue holds elements on a priority heap, which orders the elements according to their natural order or according to the comparator specified at construction time. If the queue uses natural ordering, only elements that are comparable are permitted to be inserted into the queue.

The least element of the specified ordering is the first retrieved with poll() and the greatest element is the last.

A PriorityQueue is not synchronized. If multiple threads will have to access it concurrently, use the PriorityBlockingQueue.

Summary

Public constructors

PriorityQueue()

Constructs a priority queue with an initial capacity of 11 and natural ordering.

PriorityQueue(int initialCapacity)

Constructs a priority queue with the specified capacity and natural ordering.

PriorityQueue(int initialCapacity, Comparator<? super E> comparator)

Constructs a priority queue with the specified capacity and comparator.

PriorityQueue(Collection<? extends E> c)

Constructs a priority queue that contains the elements of a collection.

PriorityQueue(PriorityQueue<? extends E> c)

Constructs a priority queue that contains the elements of another priority queue.

PriorityQueue(SortedSet<? extends E> c)

Constructs a priority queue that contains the elements of a sorted set.

Public methods

boolean add(E o)

Adds the specified object to the priority queue.

void clear()

Removes all the elements of the priority queue.

Comparator<? super E> comparator()

Gets the comparator of the priority queue.

Iterator<E> iterator()

Gets the iterator of the priority queue, which will not return elements in any specified ordering.

boolean offer(E o)

Inserts the element to the priority queue.

E peek()

Gets but does not remove the head of the queue.

E poll()

Gets and removes the head of the queue.

boolean remove(Object o)

Removes the specified object from the priority queue.

int size()

Gets the size of the priority queue.

Inherited methods

From class java.util.AbstractQueue
From class java.util.AbstractCollection
From class java.lang.Object
From interface java.util.Queue
From interface java.util.Collection
From interface java.lang.Iterable

Public constructors

PriorityQueue

Added in API level 1
PriorityQueue ()

Constructs a priority queue with an initial capacity of 11 and natural ordering.

PriorityQueue

Added in API level 1
PriorityQueue (int initialCapacity)

Constructs a priority queue with the specified capacity and natural ordering.

Parameters
initialCapacity int: the specified capacity.
Throws
IllegalArgumentException if the initialCapacity is less than 1.

PriorityQueue

Added in API level 1
PriorityQueue (int initialCapacity, 
                Comparator<? super E> comparator)

Constructs a priority queue with the specified capacity and comparator.

Parameters
initialCapacity int: the specified capacity.
comparator Comparator: the specified comparator. If it is null, the natural ordering will be used.
Throws
IllegalArgumentException if the initialCapacity is less than 1.

PriorityQueue

Added in API level 1
PriorityQueue (Collection<? extends E> c)

Constructs a priority queue that contains the elements of a collection. The constructed priority queue has the initial capacity of 110% of the size of the collection. The queue uses natural ordering to order its elements.

Parameters
c Collection: the collection whose elements will be added to the priority queue to be constructed.
Throws
ClassCastException if any of the elements in the collection are not comparable.
NullPointerException if any of the elements in the collection are null.

PriorityQueue

Added in API level 1
PriorityQueue (PriorityQueue<? extends E> c)

Constructs a priority queue that contains the elements of another priority queue. The constructed priority queue has the initial capacity of 110% of the specified one. Both priority queues have the same comparator.

Parameters
c PriorityQueue: the priority queue whose elements will be added to the priority queue to be constructed.

PriorityQueue

Added in API level 1
PriorityQueue (SortedSet<? extends E> c)

Constructs a priority queue that contains the elements of a sorted set. The constructed priority queue has the initial capacity of 110% of the size of the sorted set. The priority queue will have the same comparator as the sorted set.

Parameters
c SortedSet: the sorted set whose elements will be added to the priority queue to be constructed.

Public methods

add

Added in API level 1
boolean add (E o)

Adds the specified object to the priority queue.

Parameters
o E: the object to be added.
Returns
boolean always true.
Throws
ClassCastException if the element cannot be compared with the elements in the priority queue using the ordering of the priority queue.
NullPointerException if o is null.

clear

Added in API level 1
void clear ()

Removes all the elements of the priority queue.

comparator

Added in API level 1
Comparator<? super E> comparator ()

Gets the comparator of the priority queue.

Returns
Comparator<? super E> the comparator of the priority queue or null if the natural ordering is used.

iterator

Added in API level 1
Iterator<E> iterator ()

Gets the iterator of the priority queue, which will not return elements in any specified ordering.

Returns
Iterator<E> the iterator of the priority queue.

offer

Added in API level 1
boolean offer (E o)

Inserts the element to the priority queue.

Parameters
o E: the element to add to the priority queue.
Returns
boolean always true
Throws
ClassCastException if the element cannot be compared with the elements in the priority queue using the ordering of the priority queue.
NullPointerException if o is null.

peek

Added in API level 1
E peek ()

Gets but does not remove the head of the queue.

Returns
E the head of the queue or null if the queue is empty.

poll

Added in API level 1
E poll ()

Gets and removes the head of the queue.

Returns
E the head of the queue or null if the queue is empty.

remove

Added in API level 1
boolean remove (Object o)

Removes the specified object from the priority queue.

Parameters
o Object: the object to be removed.
Returns
boolean true if the object was in the priority queue, false if the object was not in the priority queue.

size

Added in API level 1
int size ()

Gets the size of the priority queue. If the size of the queue is greater than the Integer.MAX, then it returns Integer.MAX.

Returns
int the size of the priority queue.
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.