public class PriorityQueue
extends AbstractQueue<E> implements Serializable
| java.lang.Object | |||
| ↳ | java.util.AbstractCollection<E> | ||
| ↳ | java.util.AbstractQueue<E> | ||
| ↳ | java.util.PriorityQueue<E> | ||
基于优先级堆的无限优先级queue 。 优先级队列的元素根据它们的有序natural ordering ,或由一个Comparator在队列构造的时候提供,这取决于所使用的构造方法。 优先级队列不允许null元素。 依赖于自然顺序的优先级队列也不允许插入不可比较的对象(这样做可能导致ClassCastException )。
这个队列的头部是相对于指定顺序的最小元素。 如果多个元素的价值最小,那么头是其中一个元素 - 领带被任意破坏。 队列检索操作poll , remove , peek和element访问在队列的头部的元件。
优先级队列是无界的,但具有控制用于存储队列中元素的数组大小的内部容量 。 它总是至少与队列大小一样大。 随着元素被添加到优先级队列中,其容量会自动增加。 增长政策的细节没有说明。
该类及其迭代器实现了Collection和Iterator接口的所有可选方法。 不保证方法iterator()提供的迭代器以任何特定顺序遍历优先级队列的元素。 如果您需要有序遍历,请考虑使用Arrays.sort(pq.toArray()) 。
请注意,此实现不同步。 如果任何线程修改队列,则多个线程不应该同时访问PriorityQueue实例。 相反,请使用线程安全的PriorityBlockingQueue类。
实现注意事项:此实现提供了O(日志(n))的时间入队和出队方法( offer , poll , remove()和add ); 线性时间为remove(Object)和contains(Object)方法; 和恒定时间检索方法( peek , element ,和size )。
此课程是 Java Collections Framework的成员。
Public constructors |
|
|---|---|
PriorityQueue() 使用默认初始容量(11)创建一个 |
|
PriorityQueue(int initialCapacity) 用指定的初始容量创建一个 |
|
PriorityQueue(Comparator<? super E> comparator) 用默认的初始容量创建一个 |
|
PriorityQueue(int initialCapacity, Comparator<? super E> comparator) 用指定的初始容量创建一个 |
|
PriorityQueue(Collection<? extends E> c) 创建一个包含指定集合中元素的 |
|
PriorityQueue(PriorityQueue<? extends E> c) 创建包含指定优先级队列中元素的 |
|
PriorityQueue(SortedSet<? extends E> c) 创建一个包含指定排序集中元素的 |
|
Public methods |
|
|---|---|
boolean |
add(E e) 将指定的元素插入到此优先级队列中。 |
void |
clear() 删除此优先队列中的所有元素。 |
Comparator<? super E> |
comparator() 返回用于为了在这个队列中的元素,或比较 |
boolean |
contains(Object o) 如果此队列包含指定的元素,则返回 |
Iterator<E> |
iterator() 返回此队列中元素的迭代器。 |
boolean |
offer(E e) 将指定的元素插入到此优先级队列中。 |
E |
peek() |
E |
poll() |
boolean |
remove(Object o) 从该队列中移除指定元素的单个实例(如果存在)。 |
int |
size() 返回此集合中的元素数量。 |
final Spliterator<E> |
spliterator() 在此队列中的元素上创建 late-binding和 快速失败 |
<T> T[] |
toArray(T[] a) 返回包含此队列中所有元素的数组; 返回数组的运行时类型是指定数组的运行时类型。 |
Object[] |
toArray() 返回包含此队列中所有元素的数组。 |
Inherited methods |
|
|---|---|
java.util.AbstractQueue
|
|
java.util.AbstractCollection
|
|
java.lang.Object
|
|
java.util.Queue
|
|
java.util.Collection
|
|
java.lang.Iterable
|
|
PriorityQueue ()
使用默认初始容量(11)创建一个 PriorityQueue ,该容量根据其 natural ordering对其元素进行 排序 。
PriorityQueue (int initialCapacity)
用指定的初始容量创建一个 PriorityQueue ,该初始容量根据其 natural ordering对其元素进行 排序 。
| Parameters | |
|---|---|
initialCapacity |
int: the initial capacity for this priority queue |
| Throws | |
|---|---|
IllegalArgumentException |
if initialCapacity is less than 1 |
PriorityQueue (Comparator<? super E> comparator)
使用默认初始容量创建一个 PriorityQueue ,其元素根据指定的比较器进行排序。
| Parameters | |
|---|---|
comparator |
Comparator: the comparator that will be used to order this priority queue. If null, the natural ordering of the elements will be used. |
PriorityQueue (int initialCapacity,
Comparator<? super E> comparator)
用指定的初始容量创建一个 PriorityQueue ,根据指定的比较器对其元素进行排序。
| Parameters | |
|---|---|
initialCapacity |
int: the initial capacity for this priority queue |
comparator |
Comparator: the comparator that will be used to order this priority queue. If null, the natural ordering of the elements will be used. |
| Throws | |
|---|---|
IllegalArgumentException |
if initialCapacity is less than 1 |
PriorityQueue (Collection<? extends E> c)
创建一个包含指定集合中元素的PriorityQueue 。 如果指定的集合是SortedSet一个实例或者是另一个PriorityQueue ,则此优先级队列将按照相同的顺序排序。 否则,该优先级队列将根据其元素的natural ordering进行排序。
| Parameters | |
|---|---|
c |
Collection: 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 the specified collection or any of its elements are null |
PriorityQueue (PriorityQueue<? extends E> c)
创建包含指定优先级队列中元素的PriorityQueue 。 该优先级队列将按照与给定优先级队列相同的顺序进行排序。
| Parameters | |
|---|---|
c |
PriorityQueue: the priority queue whose elements are to be placed into this priority queue |
| Throws | |
|---|---|
ClassCastException |
if elements of c cannot be compared to one another according to c's ordering |
NullPointerException |
if the specified priority queue or any of its elements are null |
PriorityQueue (SortedSet<? extends E> c)
创建一个包含指定排序集中元素的PriorityQueue 。 这个优先级队列将按照与给定排序集合相同的顺序进行排序。
| Parameters | |
|---|---|
c |
SortedSet: the sorted set whose elements are to be placed into this priority queue |
| Throws | |
|---|---|
ClassCastException |
if elements of the specified sorted set cannot be compared to one another according to the sorted set's ordering |
NullPointerException |
if the specified sorted set or any of its elements are null |
boolean add (E e)
将指定的元素插入到此优先级队列中。
| Parameters | |
|---|---|
e |
E: the element to add |
| Returns | |
|---|---|
boolean |
true (as specified by add(E)) |
| Throws | |
|---|---|
ClassCastException |
if the specified element cannot be compared with elements currently in this priority queue according to the priority queue's ordering |
NullPointerException |
if the specified element is null |
Comparator<? super E> comparator ()
返回用于为了在这个队列中的元素,或比较 null如果此队列根据所述排序 natural ordering的元素。
| Returns | |
|---|---|
Comparator<? super E> |
the comparator used to order this queue, or null if this queue is sorted according to the natural ordering of its elements |
boolean contains (Object o)
如果此队列包含指定的元素,则返回true 。 更正式地,返回true当且仅当该队列包含至少一个元素e例如o.equals(e) 。
| Parameters | |
|---|---|
o |
Object: object to be checked for containment in this queue |
| Returns | |
|---|---|
boolean |
true if this queue contains the specified element |
Iterator<E> iterator ()
返回此队列中元素的迭代器。 迭代器不会以任何特定顺序返回元素。
| Returns | |
|---|---|
Iterator<E> |
an iterator over the elements in this queue |
boolean offer (E e)
将指定的元素插入到此优先级队列中。
| Parameters | |
|---|---|
e |
E
|
| Returns | |
|---|---|
boolean |
true (as specified by offer(E)) |
| Throws | |
|---|---|
ClassCastException |
if the specified element cannot be compared with elements currently in this priority queue according to the priority queue's ordering |
NullPointerException |
if the specified element is null |
boolean remove (Object o)
从该队列中移除指定元素的单个实例(如果存在)。 更正式地说,删除一个元素e例如o.equals(e) ,如果这个队列包含一个或多个这样的元素。 返回true当且仅当此队列包含指定的元素(或等同地,如果此队列由于调用而更改)。
| Parameters | |
|---|---|
o |
Object: element to be removed from this queue, if present |
| Returns | |
|---|---|
boolean |
true if this queue changed as a result of the call |
int size ()
返回此集合中的元素数量。 如果此集合包含多个Integer.MAX_VALUE元素,则返回Integer.MAX_VALUE 。
| Returns | |
|---|---|
int |
the number of elements in this collection |
Spliterator<E> spliterator ()
在此队列中的元素上创建 late-binding和 快速失败 Spliterator 。
该Spliterator报告SIZED , SUBSIZED ,并NONNULL 。 重写实现应记录附加特征值的报告。
| Returns | |
|---|---|
Spliterator<E> |
a Spliterator over the elements in this queue |
T[] toArray (T[] a)
返回包含此队列中所有元素的数组; 返回数组的运行时类型是指定数组的运行时类型。 返回的数组元素没有特定的顺序。 如果队列适合指定的数组,则返回其中。 否则,将使用指定数组的运行时类型和此队列的大小分配一个新数组。
如果队列符合指定数组并有空余空间(即数组的元素数多于队列数),则紧跟集合结束后的数组元素将设置为 null 。
与toArray()方法一样,此方法充当基于数组和基于集合的API之间的桥梁。 此外,该方法允许精确控制输出数组的运行时类型,并且在某些情况下可以用于节省分配成本。
假设x是已知只包含字符串的队列。 以下代码可用于将队列转储到新分配的String数组中:
String[] y = x.toArray(new String[0]); Note that
toArray(new Object[0]) is identical in function to
toArray().
| Parameters | |
|---|---|
a |
T: 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 | |
|---|---|
T[] |
an array containing all of the elements in this queue |
| Throws | |
|---|---|
ArrayStoreException |
if the runtime type of the specified array is not a supertype of the runtime type of every element in this queue |
NullPointerException |
if the specified array is null |
Object[] toArray ()
返回包含此队列中所有元素的数组。 元素没有特定的顺序。
返回的数组将是“安全”的,因为此队列不维护对它的引用。 (换句话说,这个方法必须分配一个新的数组)。 调用者可以自由修改返回的数组。
此方法充当基于数组和基于集合的API之间的桥梁。
| Returns | |
|---|---|
Object[] |
an array containing all of the elements in this queue |