简体   繁体   English

优先队列轮询

[英]PriorityQueue poll

Looks like PriorityQueue is a LIFO structure (if all the elements in it have same priority), am I right?看起来 PriorityQueue 是一个后进先出结构(如果其中的所有元素都具有相同的优先级),对吗?

 public E poll() {
    if (size == 0)
        return null;
    modCount++;

    E result = (E) queue[1];
    queue[1] = queue[size];
    queue[size--] = null;  // Drop extra ref to prevent memory leak
    if (size > 1)
        fixDown(1);

    return result;
}

The statement in the Javadoc that 'ties are broken arbitrarily' means the answer to your question is 'no'. Javadoc 中的声明“任意打破关系”意味着您的问题的答案是“否”。

From the documentation:从文档中:

An unbounded priority queue based on a priority heap.基于优先级堆的无界优先级队列。 The elements of the priority queue are ordered according to their natural ordering, or by a Comparator provided at queue construction time, depending on which constructor is used.优先级队列的元素根据它们的自然顺序进行排序,或者由队列构造时提供的比较器排序,具体取决于使用的构造函数。 A priority queue does not permit null elements.优先级队列不允许 null 个元素。 A priority queue relying on natural ordering also does not permit insertion of non-comparable objects (doing so may result in ClassCastException).依赖自然排序的优先级队列也不允许插入不可比较的对象(这样做可能会导致 ClassCastException)。

The head of this queue is the least element with respect to the specified ordering.该队列的头部是相对于指定顺序的最少元素。 If multiple elements are tied for least value, the head is one of those elements -- ties are broken arbitrarily.如果多个元素为最小值绑定,则头部是这些元素之一——任意打破绑定。 The queue retrieval operations poll, remove, peek, and element access the element at the head of the queue.队列检索操作 poll、remove、peek 和 element 访问队列头部的元素。

http://docs.oracle.com/javase/6/docs/api/java/util/PriorityQueue.html http://docs.oracle.com/javase/6/docs/api/java/util/PriorityQueue.html

Basically, there's always a "priority", by default, it's the natural ordering of elements.基本上,总是有一个“优先级”,默认情况下,它是元素的自然顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM