E - 此集合中保存的元素的类型
public class PriorityBlockingQueue<E> extends AbstractQueue<E> implements BlockingQueue<E>, Serializable
PriorityQueue类相同的排序规则,并提供阻塞检索操作。
虽然这个队列在逻辑上是无界的,但由于资源耗尽,尝试的添加可能会失败(导致OutOfMemoryError )。
这个类不允许null元素。
根据natural ordering的优先级队列也不允许插入不可比较的对象(这样做在ClassCastException )。
该类及其迭代器实现Collection和Iterator接口的所有可选方法。 方法iterator()中提供的迭代器不能保证以任何特定的顺序遍历PriorityBlockingQueue的元素。 如果需要排序遍历,请考虑使用Arrays.sort(pq.toArray()) 。 此外,方法drainTo可以用于以优先级顺序移除一些或所有元素并将它们放置在另一集合中。
这个类的操作不会保证等同优先级的元素的排序。 如果需要强制执行排序,您可以定义自定义类或比较器,它们使用辅助键来破坏主优先级值的关系。 例如,这里是一个适用于先进先出的打破破坏类似元素的课程。 要使用它,您将插入一个new FIFOEntry(anEntry)而不是一个简单的条目对象。
class FIFOEntry<E extends Comparable<? super E>> implements Comparable<FIFOEntry<E>> { static final AtomicLong seq = new AtomicLong(0); final long seqNum; final E entry; public FIFOEntry(E entry) { seqNum = seq.getAndIncrement(); this.entry = entry; } public E getEntry() { return entry; } public int compareTo(FIFOEntry<E> other) { int res = entry.compareTo(other.entry); if (res == 0 && other.entry != this.entry) res = (seqNum < other.seqNum ? -1 : 1); return res; } }
| Constructor and Description |
|---|
PriorityBlockingQueue()
创建一个 PriorityBlockingQueue ,具有默认的初始容量(11),根据它们的natural ordering对其元素进行排序 。
|
PriorityBlockingQueue(Collection<? extends E> c)
创建一个
PriorityBlockingQueue集合中的元素的PriorityBlockingQueue。
|
PriorityBlockingQueue(int initialCapacity)
创建具有 PriorityBlockingQueue初始容量的PriorityBlockingQueue,根据它们的natural ordering对其元素进行排序 。
|
PriorityBlockingQueue(int initialCapacity, Comparator<? super E> comparator)
创建具有
PriorityBlockingQueue初始容量的PriorityBlockingQueue,根据指定的比较器对其元素进行排序。
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
add(E e)
将指定的元素插入到此优先级队列中。
|
void |
clear()
从这个队列中原子地删除所有的元素。
|
Comparator<? super E> |
comparator()
返回用于对此队列中的元素进行排序的比较器,或 null如果此队列使用其元素的natural ordering 。
|
boolean |
contains(Object o)
如果此队列包含指定的元素,则返回
true 。
|
int |
drainTo(Collection<? super E> c)
从该队列中删除所有可用的元素,并将它们添加到给定的集合中。
|
int |
drainTo(Collection<? super E> c, int maxElements)
最多从该队列中删除给定数量的可用元素,并将它们添加到给定的集合中。
|
Iterator<E> |
iterator()
返回此队列中的元素的迭代器。
|
boolean |
offer(E e)
将指定的元素插入到此优先级队列中。
|
boolean |
offer(E e, long timeout, TimeUnit unit)
将指定的元素插入到此优先级队列中。
|
E |
peek()
检索但不删除此队列的头,如果此队列为空,则返回
null 。
|
E |
poll()
检索并删除此队列的头部,如果此队列为空,则返回
null 。
|
E |
poll(long timeout, TimeUnit unit)
检索并删除此队列的头,等待指定的等待时间(如有必要)使元素变为可用。
|
void |
put(E e)
将指定的元素插入到此优先级队列中。
|
int |
remainingCapacity()
总是返回
Integer.MAX_VALUE ,因为
PriorityBlockingQueue没有容量限制。
|
boolean |
remove(Object o)
从该队列中删除指定元素的单个实例(如果存在)。
|
int |
size()
返回此集合中的元素数。
|
Spliterator<E> |
spliterator()
返回此队列中的元素 Spliterator 。
|
E |
take()
检索并删除此队列的头,如有必要,等待元素可用。
|
Object[] |
toArray()
返回一个包含此队列中所有元素的数组。
|
<T> T[] |
toArray(T[] a)
返回一个包含此队列中所有元素的数组;
返回的数组的运行时类型是指定数组的运行时类型。
|
String |
toString()
返回此集合的字符串表示形式。
|
addAll, element, removecontainsAll, isEmpty, removeAll, retainAllclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitaddAll, containsAll, equals, hashCode, isEmpty, parallelStream, removeAll, removeIf, retainAll, streampublic PriorityBlockingQueue()
PriorityBlockingQueue ,其默认初始容量(11)根据其natural ordering对其元素进行排序 。
public PriorityBlockingQueue(int initialCapacity)
PriorityBlockingQueue ,具有指定的初始容量,根据它们的natural ordering对其元素进行排序 。
initialCapacity - 此优先级队列的初始容量
IllegalArgumentException - 如果
initialCapacity小于1
public PriorityBlockingQueue(int initialCapacity,
Comparator<? super E> comparator)
PriorityBlockingQueue初始容量的PriorityBlockingQueue,根据指定的比较器对其元素进行排序。
initialCapacity - 此优先级队列的初始容量
comparator - 将用于订购此优先级队列的比较器。
如果null ,该natural ordering的元素将被使用。
IllegalArgumentException - 如果
initialCapacity小于1
public PriorityBlockingQueue(Collection<? extends E> c)
PriorityBlockingQueue集合中的元素的PriorityBlockingQueue。
如果指定的集合是SortedSet或PriorityQueue ,则该优先级队列将按照相同的顺序进行排序。
否则,此优先级队列将根据其元素的natural ordering进行排序。
c - 要将元素放入此优先级队列的集合
ClassCastException - 如果指定集合的元素不能根据优先级队列的顺序相互比较
NullPointerException - 如果指定的集合或其任何元素为空
public boolean add(E e)
add在界面
Collection<E>
add在界面
BlockingQueue<E>
add在界面
Queue<E>
add在类别
AbstractQueue<E>
e - 要添加的元素
true (由
Collection.add(E)指定 )
ClassCastException - 如果指定的元素不能根据优先级队列的顺序与当前在优先级队列中的元素进行比较
NullPointerException - 如果指定的元素为空
public boolean offer(E e)
false 。
offer在接口
BlockingQueue<E>
offer在界面
Queue<E>
e - 要添加的元素
true (由
Queue.offer(E)指定 )
ClassCastException - 如果指定的元素不能根据优先级队列的顺序与当前在优先级队列中的元素进行比较
NullPointerException - 如果指定的元素为空
public void put(E e)
put在界面
BlockingQueue<E>
e - 要添加的元素
ClassCastException - 如果指定的元素不能根据优先级队列的顺序与当前优先级队列中的元素进行比较
NullPointerException - 如果指定的元素为空
public boolean offer(E e, long timeout, TimeUnit unit)
false 。
offer在界面
BlockingQueue<E>
e - 要添加的元素
timeout - 该参数被忽略,因为该方法从不阻止
unit - 该参数被忽略,因为该方法从不阻止
true (由
BlockingQueue.offer指定 )
ClassCastException - 如果指定的元素不能根据优先级队列的顺序与当前在优先级队列中的元素进行比较
NullPointerException - 如果指定的元素为空
public E take() throws InterruptedException
BlockingQueue复制
take在界面
BlockingQueue<E>
InterruptedException - 如果等待中断
public E poll(long timeout, TimeUnit unit) throws InterruptedException
BlockingQueue复制
poll在界面
BlockingQueue<E>
timeout - 放弃之前等待多久,以
unit为单位
unit - a
TimeUnit确定如何解释
timeout参数
null如果在元素可用之前经过指定的等待时间
InterruptedException - 如果在等待时中断
public Comparator<? super E> comparator()
null如果此队列使用其元素的natural ordering 。
null
public int size()
Collection复制
size在界面
Collection<E>
size在类别
AbstractCollection<E>
public int remainingCapacity()
Integer.MAX_VALUE ,因为
PriorityBlockingQueue没有容量限制。
remainingCapacity在界面
BlockingQueue<E>
Integer.MAX_VALUE
public boolean remove(Object o)
e ,使o.equals(e) ,如果这个队列包含一个或多个这样的元素。
返回true当且仅当此队列包含指定的元素(或等效地,如果此队列作为调用的结果而更改)。
remove在界面
Collection<E>
remove在界面
BlockingQueue<E>
remove在类别
AbstractCollection<E>
o - 要从此队列中删除的元素(如果存在)
true如果此队列由于调用而更改
public boolean contains(Object o)
true 。
更正式地,返回true当且仅当这个队列包含至少一个元素e使得o.equals(e) 。
contains在界面
Collection<E>
contains在界面
BlockingQueue<E>
contains在类别
AbstractCollection<E>
o - 要在此队列中检查包含的对象
true如果此队列包含指定的元素
public Object[] toArray()
返回的数组将是“安全的”,因为该队列不保留对它的引用。 (换句话说,这个方法必须分配一个新的数组)。 因此,调用者可以自由地修改返回的数组。
此方法充当基于阵列和基于集合的API之间的桥梁。
toArray在界面
Collection<E>
toArray在类别
AbstractCollection<E>
public String toString()
AbstractCollection复制
String.valueOf(Object) 。
toString在类别
AbstractCollection<E>
public int drainTo(Collection<? super E> c)
BlockingQueue复制
c添加元素时遇到的c可能会导致在抛出关联的异常时,元素既不在两个集合中,也可能不是两个集合。
尝试将队列排入自身会导致IllegalArgumentException 。
此外,如果在操作进行中修改了指定的集合,则此操作的行为是未定义的。
drainTo在界面
BlockingQueue<E>
c - 将元素传输到的集合
UnsupportedOperationException - 如果指定集合不支持元素的添加
ClassCastException - 如果此队列的元素的类阻止将其添加到指定的集合
NullPointerException - 如果指定的集合为空
IllegalArgumentException - 如果指定的集合是此队列,或此队列的某个元素的某些属性会阻止将其添加到指定的集合
public int drainTo(Collection<? super E> c, int maxElements)
BlockingQueue复制
c添加元素时遇到的c可能会导致元素在抛出关联的异常时既不在两个集合中,也可能不是两个集合。
尝试将队列排入自身导致IllegalArgumentException 。
此外,如果在操作进行中修改了指定的集合,则此操作的行为是未定义的。
drainTo在界面
BlockingQueue<E>
c - 传输元素的集合
maxElements - 要传输的元素的最大数量
UnsupportedOperationException - 如果指定集合不支持添加元素
ClassCastException - 如果此队列的元素的类阻止将其添加到指定的集合
NullPointerException - 如果指定的集合为空
IllegalArgumentException - 如果指定的集合是此队列,或此队列的某个元素的某些属性阻止将其添加到指定的集合
public void clear()
clear在界面
Collection<E>
clear在类别
AbstractQueue<E>
public <T> T[] toArray(T[] a)
如果这个队列符合指定的数组空间(即,该数组具有比该队列更多的元素),则队列结束后的数组中的元素设置为null 。
像toArray()方法一样,此方法充当基于数组和基于集合的API之间的桥梁。 此外,该方法允许精确地控制输出阵列的运行时类型,并且在某些情况下可以用于节省分配成本。
假设x是一个已知只包含字符串的队列。 以下代码可用于将队列转储到新分配的数组中: String :
String[] y = x.toArray(new String[0]);
请注意, toArray(new Object[0])的功能与toArray() 。
toArray在界面
Collection<E>
toArray在类别
AbstractCollection<E>
T - 包含集合的数组的运行时类型
a - 要存储队列的元素的阵列,如果它足够大;
否则,为此目的分配相同运行时类型的新数组
ArrayStoreException - 如果指定数组的运行时类型不是此队列中每个元素的运行时类型的超类型
NullPointerException - 如果指定的数组为空
public Iterator<E> iterator()
iterator在界面
Iterable<E>
iterator在界面
Collection<E>
iterator在类别
AbstractCollection<E>
public Spliterator<E> spliterator()
Spliterator 。
返回的拼接器是weakly consistent 。
Spliterator报告Spliterator.SIZED和Spliterator.NONNULL 。
spliterator在界面
Iterable<E>
spliterator在界面
Collection<E>
Spliterator另外报道
Spliterator.SUBSIZED 。
Spliterator在这个队列中的元素
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.