Class Overview
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 stored at the head of the
queue and the greatest element is stored at the tail of the queue.
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.
|
[Expand]
Inherited Methods |
From class java.util.AbstractQueue
boolean
|
add(E o)
Adds an element to the queue.
|
boolean
|
addAll(Collection<? extends E> c)
Adds all the elements of a collection to the queue.
|
void
|
clear()
Removes all elements of the queue, leaving it empty.
|
E
|
element()
Returns but does not remove the element at the head of the queue.
|
E
|
remove()
Removes the element at the head of the queue and returns it.
|
|
From class java.util.AbstractCollection
boolean
|
add(E object)
Attempts to add object to the contents of this
Collection (optional).
|
boolean
|
addAll(Collection<? extends E> collection)
Attempts to add all of the objects contained in collection
to the contents of this Collection (optional).
|
void
|
clear()
Removes all elements from this Collection , leaving it empty (optional).
|
boolean
|
contains(Object object)
Tests whether this Collection contains the specified object.
|
boolean
|
containsAll(Collection<?> collection)
Tests whether this Collection contains all objects contained in the
specified Collection .
|
boolean
|
isEmpty()
Returns if this Collection contains no elements.
|
abstract
Iterator<E>
|
iterator()
Returns an instance of Iterator that may be used to access the
objects contained by this Collection .
|
boolean
|
remove(Object object)
Removes one instance of the specified object from this Collection if one
is contained (optional).
|
boolean
|
removeAll(Collection<?> collection)
Removes all occurrences in this Collection of each object in the
specified Collection (optional).
|
boolean
|
retainAll(Collection<?> collection)
Removes all objects from this Collection that are not also found in the
Collection passed (optional).
|
abstract
int
|
size()
Returns a count of how many objects this Collection contains.
|
<T>
T[]
|
toArray(T[] contents)
Returns an array containing all elements contained in this Collection .
|
Object[]
|
toArray()
Returns a new array containing all elements contained in this Collection .
|
String
|
toString()
Returns the string representation of this Collection .
|
|
From class java.lang.Object
Object
|
clone()
Creates and returns a copy of this Object .
|
boolean
|
equals(Object o)
Compares this instance with the specified object and indicates if they
are equal.
|
void
|
finalize()
Is called before the object's memory is being reclaimed by the VM.
|
final
Class<? extends Object>
|
getClass()
Returns the unique instance of Class which represents this
object's class.
|
int
|
hashCode()
Returns an integer hash code for this object.
|
final
void
|
notify()
Causes a thread which is waiting on this object's monitor (by means of
calling one of the wait() methods) to be woken up.
|
final
void
|
notifyAll()
Causes all threads which are waiting on this object's monitor (by means
of calling one of the wait() methods) to be woken up.
|
String
|
toString()
Returns a string containing a concise, human-readable description of this
object.
|
final
void
|
wait(long millis, int nanos)
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the
specified timeout expires.
|
final
void
|
wait(long millis)
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the
specified timeout expires.
|
final
void
|
wait()
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object.
|
|
From interface java.lang.Iterable
|
From interface java.util.Collection
abstract
boolean
|
add(E object)
Attempts to add object to the contents of this
Collection (optional).
|
abstract
boolean
|
addAll(Collection<? extends E> collection)
Attempts to add all of the objects contained in Collection
to the contents of this Collection (optional).
|
abstract
void
|
clear()
Removes all elements from this Collection , leaving it empty (optional).
|
abstract
boolean
|
contains(Object object)
Tests whether this Collection contains the specified object.
|
abstract
boolean
|
containsAll(Collection<?> collection)
Tests whether this Collection contains all objects contained in the
specified Collection .
|
abstract
boolean
|
equals(Object object)
Compares the argument to the receiver, and returns true if they represent
the same object using a class specific comparison.
|
abstract
int
|
hashCode()
Returns an integer hash code for the receiver.
|
abstract
boolean
|
isEmpty()
Returns if this Collection contains no elements.
|
abstract
Iterator<E>
|
iterator()
Returns an instance of Iterator that may be used to access the
objects contained by this Collection .
|
abstract
boolean
|
remove(Object object)
Removes one instance of the specified object from this Collection if one
is contained (optional).
|
abstract
boolean
|
removeAll(Collection<?> collection)
Removes all occurrences in this Collection of each object in the
specified Collection (optional).
|
abstract
boolean
|
retainAll(Collection<?> collection)
Removes all objects from this Collection that are not also found in the
Collection passed (optional).
|
abstract
int
|
size()
Returns a count of how many objects this Collection contains.
|
abstract
<T>
T[]
|
toArray(T[] array)
Returns an array containing all elements contained in this Collection .
|
abstract
Object[]
|
toArray()
Returns a new array containing all elements contained in this Collection .
|
|
From interface java.util.Queue
abstract
E
|
element()
Gets but does not remove the element at the head of the queue.
|
abstract
boolean
|
offer(E o)
Inserts the specified element into the queue provided that the condition
allows such an operation.
|
abstract
E
|
peek()
Gets but does not remove the element at the head of the queue.
|
abstract
E
|
poll()
Gets and removes the element at the head of the queue, or returns null if there is no element in the queue.
|
abstract
E
|
remove()
Gets and removes the element at the head of the queue.
|
|
Public Constructors
public
PriorityQueue
()
Constructs a priority queue with an initial capacity of 11 and natural
ordering.
public
PriorityQueue
(int initialCapacity)
Constructs a priority queue with the specified capacity and natural
ordering.
Parameters
initialCapacity
| the specified capacity. |
public
PriorityQueue
(int initialCapacity, Comparator<? super E> comparator)
Constructs a priority queue with the specified capacity and comparator.
Parameters
initialCapacity
| the specified capacity. |
comparator
| the specified comparator. If it is null, the natural ordering
will be used. |
public
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
| the collection whose elements will be added to the priority
queue to be constructed. |
public
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
| the priority queue whose elements will be added to the
priority queue to be constructed. |
public
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
| the sorted set whose elements will be added to the priority
queue to be constructed. |
Public Methods
public
boolean
add
(E o)
Adds the specified object to the priority queue.
Parameters
o
| the object to be added. |
Throws
ClassCastException
| if the element cannot be compared with the elements in the
priority queue using the ordering of the priority queue. |
public
void
clear
()
Removes all the elements of the priority queue.
public
Comparator<? super E>
comparator
()
Gets the comparator of the priority queue.
Returns
- the comparator of the priority queue or null if the natural
ordering is used.
public
Iterator<E>
iterator
()
Gets the iterator of the priority queue, which will not return elements
in any specified ordering.
Returns
- the iterator of the priority queue.
public
boolean
offer
(E o)
Inserts the element to the priority queue.
Parameters
o
| the element to add to the priority queue. |
Throws
ClassCastException
| if the element cannot be compared with the elements in the
priority queue using the ordering of the priority queue. |
public
E
peek
()
Gets but does not remove the head of the queue.
Returns
- the head of the queue or null if the queue is empty.
public
E
poll
()
Gets and removes the head of the queue.
Returns
- the head of the queue or null if the queue is empty.
public
boolean
remove
(Object o)
Removes the specified object from the priority queue.
Parameters
o
| the object to be removed. |
Returns
- true if the object was in the priority queue, false if the object
was not in the priority queue.
public
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
- the size of the priority queue.