java - 如何创建一个带有新比较器且没有指定初始容量的 PriorityQueue?

标签 java priority-queue

在 Java 中,我不知道如何使用新的比较器创建一个新的 PriorityQueue 但没有给定队列长度?如何创建它?

我知道我可以写:

Queue<Node> theQueue = new PriorityQueue<Node>(15,new Comparator<Node>();

但我希望队列能像LinkedList那样工作,我的意思是它的长度不是固定的,我该如何声明呢?

最佳答案

现代答案,截至 2021 年:https://stackoverflow.com/a/30015986/139010


Java-8 之前的答案,供后人引用:

没有这样的构造函数。根据 JavaDocs,the default capacity is 11 ,因此您可以针对无参数 PriorityQueue 构造函数的类似行为指定它:

Queue<Node> theQueue = new PriorityQueue<Node>(11,new Comparator<Node>());

是的,the queue will grow if it needs to.

A priority queue is unbounded, but has an internal capacity governing the size of an array used to store the elements on the queue. It is always at least as large as the queue size. As elements are added to a priority queue, its capacity grows automatically. The details of the growth policy are not specified.x

关于java - 如何创建一个带有新比较器且没有指定初始容量的 PriorityQueue?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15098731/

相关文章:

java - 如果 REST Controller 类和接口(interface)具有使用 @HystrixCommand 注释的 API,则不会加载所有 REST API

java - 为什么priorityQueue不对元素进行排序

c++ - 在优先队列中随机访问

c++是否有默认数据类以合理的速度进行基于排序索引的访问?

C++ 结构列表 push_back 的问题

java - antlr4:ATN 版本 2 预计 3

java - java中文件路径的Windows转义序列问题

java - Java 中的响应字符串 (http)

java - 定位服务还是定位线程?

c - 重复的 typedef 错误