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;
}
}
这个班的一员 Java Collections Framework。
| Constructor and Description |
|---|
PriorityBlockingQueue()
创建一个默认的初始容量
PriorityBlockingQueue(11),命令其元素按其
natural ordering。
|
PriorityBlockingQueue(Collection<? extends E> c)
创建一个
PriorityBlockingQueue包含在指定集合的元素。
|
PriorityBlockingQueue(int initialCapacity)
创建一个具有指定的初始容量,命令其元素按其
natural ordering
PriorityBlockingQueue。
|
PriorityBlockingQueue(int initialCapacity, Comparator<? super E> comparator)
创建一个具有指定的初始容量,命令其元素根据指定的比较器
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。
initialCapacity -优先级队列的初始容量
comparator -比较器可用于订单优先级队列。如果
null,元素的
natural ordering将使用。
IllegalArgumentException -如果
initialCapacity小于1
public PriorityBlockingQueue(Collection<? extends E> c)
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 -如果指定元素为null
public boolean offer(E e)
false。
offer 接口
BlockingQueue<E>
offer 接口
Queue<E>
e -元素添加
true(由
Queue.offer(E)指定)
ClassCastException如果不能指定元素将按照优先级队列的排序目前优先级队列中的元素比较
NullPointerException -如果指定元素为null
public void put(E e)
put 接口
BlockingQueue<E>
e -元素添加
ClassCastException如果不能指定元素将按照优先级队列的排序目前优先级队列中的元素比较
NullPointerException -如果指定元素为null
public boolean offer(E e, long timeout, TimeUnit unit)
false。
offer 接口
BlockingQueue<E>
e -元素添加
timeout -忽略此参数为方法不块
unit -忽略此参数为方法不块
true(由
BlockingQueue.offer指定)
ClassCastException如果不能指定元素将按照优先级队列的排序目前优先级队列中的元素比较
NullPointerException -如果指定元素为null
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 -
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()
返回的数组将是“安全”的,在这个队列中没有引用它的引用。(换句话说,这种方法必须分配一个新的数组)。因此,调用方可以自由修改返回的数组。
此方法作为基于数组和基于集合的原料药之间的桥梁。
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添加元素可能会导致元素在没有遭遇过失败,或集合时相关的异常被抛出。试图排队列本身造成
IllegalArgumentException。此外,当操作正在进行中时,此操作的行为是不确定的,如果指定的集合被修改了。
drainTo 接口
BlockingQueue<E>
c -收集传递元素
UnsupportedOperationException如果添加元素不按指定集合的支持
ClassCastException -如果该队列的元素类型阻止其加入指定集合
NullPointerException -如果指定集合为空
IllegalArgumentException -如果指定集合的队列,该队列或一个元素的一些特性可以防止它被添加到指定的集合
public int drainTo(Collection<? super E> c, int maxElements)
BlockingQueue
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()
返回的迭代器是weakly consistent。
iterator 接口
Iterable<E>
iterator 接口
Collection<E>
iterator 方法重写,继承类
AbstractCollection<E>
public Spliterator<E> spliterator()
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.