- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我运行以下优先级队列测试时:
class Run {
public static void main(String[] args) {
PriorityQueue<Entry> q = new PriorityQueue<>(8, Collections.reverseOrder(new Comparator<Entry>() {
@Override
public int compare(Entry o1, Entry o2) {
return Integer.compare(o1.getValue(), o2.getValue());
}
}));
q.offer(new Entry(100));
q.offer(new Entry(0));
q.offer(new Entry(1));
q.offer(new Entry(-1));
q.offer(new Entry(0));
q.offer(new Entry(1));
q.offer(new Entry(-100));
q.offer(new Entry(100));
while (q.peek() != null) {
System.out.println(q.poll());
}
}
private static class Entry {
private static int GLOBAL_ID = 0;
private final int value, id;
public Entry(int value) {
this.value = value;
id = GLOBAL_ID++;
}
public int getValue() {
return value;
}
@Override
public String toString() {
return "Entry[" + id + ", value = " + value + ']';
}
}
我得到以下结果:
Entry[0, value = 100]
Entry[7, value = 100]
Entry[2, value = 1]
Entry[5, value = 1]
Entry[4, value = 0]
Entry[1, value = 0]
Entry[3, value = -1]
Entry[6, value = -100]
我希望相同的元素以与输入相同的顺序输出,因此当在条目 7 之前提供条目 0 时,它也会在轮询 7 之前轮询。但为什么条目 4 突然在条目 1 之前被轮询?是错误的 Comparator
还是 PriorityQueue
不能保证确定性行为?
最佳答案
它是不确定的。
来自documentation for PriorityQueue
If multiple elements are tied for least value, the head is one of those elements -- ties are broken arbitrarily.
关于java - Java 中的 PriorityQueue 不是确定性的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27865331/
采用另一个优先级队列的 java API PriorityQueue 构造函数是否会破坏参数?如果是这样,它的clone()方法足以创建浅拷贝吗? 最佳答案 不,它不是破坏性的。几乎所有集合类都有复制
我正在尝试复制一个 PriorityQueue 对象。 我的目标是在不修改我原来的 PriorityQueue 的情况下更改我的 Copy 的某些对象 为了这样做,我复制了我的 PriorityQue
我正在解决leetcode的Merge K Sorted Lists problem . 使用 Python2 的 Queue 模块中的 PriorityQueue 的相同算法会为 Python3 的
当 PriorityQueue.size() > 0 在 Android 上时,当 PriorityQueue.peek() 返回 null 时,我遇到了问题。 我认为这可能是设备问题。有人有什么想法
我有一个asyncio.PriorityQueue,用作网络爬虫的URL队列,当我调用url_queue.get时,得分最低的URL首先从队列中删除()。当队列达到 maxsize 项时,默认行为是阻
完全二叉树 一棵深度为k的有n个结点的 二叉树 ,对树中的结点按从上至下、从左到右的顺序进行编号,如果编号为i(1≤i≤n)的结点与 满二叉树 中编号为i的结点在二叉树中的位置相同,则这棵
我很难理解 priorityqueue 如何使用 compareTo 方法对其内容进行排序。 我在上一门名为 Node.js 的类(class)。它有 4 个字段。 private char char
我目前有一种方法使用 scala.collection.mutable.PriorityQueue 按特定顺序组合元素。例如代码看起来有点像这样: def process[A : Ordering]
没有提供自定义比较器,优先级队列按升序插入元素,但是,在删除特定元素后,顺序会发生变化。 PriorityQueue pq = new PriorityQueue<>(); pq.add(10); p
这个问题已经有答案了: The built-in iterator for java's PriorityQueue does not traverse the data structure in a
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我指的是此博客上列出的代码:https://strstr.io/Leetcode1054-Distant-Barcodes/ 我在这里复制这段代码 class Solution { publi
这个问题已经有答案了: Why does PriorityQueue.toString return the wrong element order? [duplicate] (4 个回答) 已关闭
我有一个比较器类 NComparator,它比较 2 个 Node 对象并返回 1、-1 或 0。 我初始化了一个初始容量为 100 的 PriorityQueue 和那个 NComparator。
当项目是整数与字符串时,PriorityQueue 的不同行为让我非常困惑。但在解决这个问题之前,我想了解以下行为(使用项目作为整数)。 假设我有一个包含以下数据的优先级队列(对于每个元素,第一个值是
我有使用 PriorityQueue 的程序。 poll() 没有给出队列中的所有值。 class Coffee { public static void main(String[] args
所以我正在尝试构建我的第一个 prim 算法,为此我根据其权重按优先级对边缘进行排序。 所以我认为如果我使用优先级队列会很有帮助,为此我需要让我的边缘实现 Comparable<> 接口(interf
编译器(Java 8)提示以下代码没有合适的构造函数: PriorityQueue heap = new PriorityQueue((ListNode n1, ListNode n2) -> n1.
我有一个优先级队列,我在其中添加一个节点对象,其中节点应按它们包含的值排序。由于某种原因,优先级队列不会在添加时对节点进行排序。如果有人能发现其中的问题或有任何指导,我很感激。这是一个简短的示例: P
这是我在这里发表的第一篇文章,因此请随时为我指出关于在这里提出问题的正确方向。 我的问题出在 java.util.PriorityQueue 上。 我有一个初始化的队列; myComparab
我是一名优秀的程序员,十分优秀!