带有自定义匿名比较器的 Java 优先级队列
如果这是一个尝试过的问题,请原谅我,但我有点难以弄清楚。
我目前有一个节点类,每个“节点”都是迷宫中的一个正方形。我正在尝试实现 A* 算法,因此每个节点内部都会有一个 f-cost (int) 数据成员。我想知道是否有一种方法可以创建这些节点的优先级队列,并将 f-cost 变量设置为比较器?
我在网上查看了示例,但我所能找到的只是字符串优先级队列。我可以为 Node 类实现 Comparator 吗?这允许我访问存储在其中的数据成员吗?
非常感谢!
Forgive me if this is a tried question, but I'm having a little difficulty figuring it out.
I currently have a class Node, and each 'node' is a square in a maze. I'm trying to implement the A* algorithm, so each of these nodes will have an f-cost (int) data member inside of it. I was wondering if there's a way that I can create a priority queue of these nodes, and set up the f-cost variable as the comparator?
I've looked at examples online, but all I can find are String priority queues. Can I implement Comparator for the Node class? Would this allow me to access the data member stored inside it?
Many Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
绝对地。
您可以使用基于传递给构造函数的匿名
Comparator
的PriorityQueue
:如果您的
Node
类已经实现了Comparable
您甚至不需要定义新的Comparator
,因为默认情况下将使用该顺序。除非使用任何其他方法,否则将使用对象之间的自然顺序。Absolutely.
You can use a
PriorityQueue
based on an anonymousComparator
passed to the constructor:If your
Node
class already implementsComparable
you don't even need to define a newComparator
, as that order will be used by default. Barring any other method, the natural ordering between objects will be used.这个问题不久前得到了回答,我想提供一些可用的新选项。
1) 如果您的 Node 类没有实现 Comparator 接口并且您不想(或不能)添加它,则使用 lambda:
2) 简单的逆序方法(需要 Node 实现 Comparator接口):
3)使用实用函数:
This question was answered a while ago and I want to give some new options that are available.
1) Using lambda in case if your
Node
class doesn't implement Comparator interface and you don't want (or can't) add it:2) Simple reverse order approach (require Node to implement Comparator interface):
3) Using utility function:
如果
compareTo 返回负整数,则表示“小于”,0 表示“等于”,1 表示“大于”,
只需一个函数即可使用 PriorityQueue。
编辑:比较是另一种方式,我搞砸了。 -1< | 0 = | 1>由于某种原因,我已经从右到左阅读了这些内容。
}
if compareTo returns a negative int, it means "less than", 0 means "equals", 1 means "greater than"
that one function is all you need to be able to use PriorityQueue.
EDIT: comparison is other way, i messed that up. -1 < | 0 = | 1 > i alreays read those right to left for some reason.
来自 Java 文档:
,PriorityQueues 支持通用数据类型。因此,如果您在 Node 类中实现
Comparable
,那么您就可以创建一个PriorityQueue
并正常使用它。或者,还有一个构造函数
PriorityQueue(int initialCapacity, Comparator comparator)
,它将 Comparator 作为PriorityQueue
构造函数的一部分。如果您更喜欢此方法,则您的节点类不需要包含继承Comparable
时所需的额外代码。From the Javadocs:
Furthermore, PriorityQueues support generic data types. Therefore, if you implement
Comparable
in your Node class, then you can create aPriorityQueue<Node>
and use it normally.Alternately, there is a constructor
PriorityQueue(int initialCapacity, Comparator<? super E> comparator)
that takes in a Comparator as part of thePriorityQueue
constructor. If you prefer this method, your node class does not need to contain the extra code needed when inheritingComparable
.java.util中有一个PriorityQueue类。您可以使用它,它将使用自然排序(Node 实现 Comparable)或构造函数中提供的比较器(如果您不希望在 Node 类中使用该代码)。任何类都可以访问另一个类中的任何数据,只要您通过将字段设为非私有(可能是糟糕的 OOP 风格)或提供访问器方法 public int getG()、public int getH()、public int getF() 来允许它。
There is a PriorityQueue class in java.util. You can use that and it will either use natural ordering (Node implements Comparable) or a comparator supplied in the constructor (if you don't want that code inside your Node class). Any class can access any data inside another as long as you allow it either by making a field non-private (potentially bad OOP style) or supplying an accessor method public int getG(), public int getH(), public int getF().