带有自定义匿名比较器的 Java 优先级队列

发布于 2024-08-27 06:39:41 字数 259 浏览 8 评论 0原文

如果这是一个尝试过的问题,请原谅我,但我有点难以弄清楚。

我目前有一个节点类,每个“节点”都是迷宫中的一个正方形。我正在尝试实现 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

白衬杉格子梦 2024-09-03 06:39:41

绝对地。

您可以使用基于传递给构造函数的匿名 ComparatorPriorityQueue

int initCapacity = 10;
PriorityQueue<Node> pq = new PriorityQueue<Node>(initCapacity, new Comparator<Node>() {
    public int compare(Node n1, Node n2) {
        // compare n1 and n2
    }
});
// use pq as you would use any PriorityQueue

如果您的 Node 类已经实现了 Comparable您甚至不需要定义新的Comparator,因为默认情况下将使用该顺序。除非使用任何其他方法,否则将使用对象之间的自然顺序。

Absolutely.

You can use a PriorityQueue based on an anonymous Comparator passed to the constructor:

int initCapacity = 10;
PriorityQueue<Node> pq = new PriorityQueue<Node>(initCapacity, new Comparator<Node>() {
    public int compare(Node n1, Node n2) {
        // compare n1 and n2
    }
});
// use pq as you would use any PriorityQueue

If your Node class already implements Comparable you don't even need to define a new Comparator, as that order will be used by default. Barring any other method, the natural ordering between objects will be used.

深海蓝天 2024-09-03 06:39:41

这个问题不久前得到了回答,我想提供一些可用的新选项。

1) 如果您的 Node 类没有实现 Comparator 接口并且您不想(或不能)添加它,则使用 lambda:

  new PriorityQueue<>((node1, node2) -> Integer.compare(node1.getCost(), node2.getCost()));

2) 简单的逆序方法(需要 Node 实现 Comparator接口):

  new PriorityQueue<>(Comparator.reverseOrder());

3)使用实用函数:

  new PriorityQueue<>(NodeUtil::customCompare);

  public static int customCompare(Node n1, Node n2) {
       return Integer.compare(n1.getCost(), n2.getCost());
  }

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:

  new PriorityQueue<>((node1, node2) -> Integer.compare(node1.getCost(), node2.getCost()));

2) Simple reverse order approach (require Node to implement Comparator interface):

  new PriorityQueue<>(Comparator.reverseOrder());

3) Using utility function:

  new PriorityQueue<>(NodeUtil::customCompare);

  public static int customCompare(Node n1, Node n2) {
       return Integer.compare(n1.getCost(), n2.getCost());
  }
浴红衣 2024-09-03 06:39:41
public class Node implements Comparable<Node>{

    public int compareTo(Node o) {
         // your comparative function
         return 0;
    }

如果

compareTo 返回负整数,则表示“小于”,0 表示“等于”,1 表示“大于”,

只需一个函数即可使用 PriorityQueue。

编辑:比较是另一种方式,我搞砸了。 -1< | 0 = | 1>由于某种原因,我已经从右到左阅读了这些内容。

public class Node implements Comparable<Node>{

    public int compareTo(Node o) {
         // your comparative function
         return 0;
    }

}

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.

思慕 2024-09-03 06:39:41

来自 Java 文档:

基于a的无界优先级队列
优先级堆。这个队列订单
元素按顺序排列
施工时指定,其中
是根据他们指定的
自然顺序(参见可比),或
根据比较器

,PriorityQueues 支持通用数据类型。因此,如果您在 Node 类中实现 Comparable,那么您就可以创建一个 PriorityQueue 并正常使用它。

或者,还有一个构造函数 PriorityQueue(int initialCapacity, Comparator comparator),它将 Comparator 作为 PriorityQueue 构造函数的一部分。如果您更喜欢此方法,则您的节点类不需要包含继承 Comparable 时所需的额外代码。

From the Javadocs:

An unbounded priority queue based on a
priority heap. This queue orders
elements according to an order
specified at construction time, which
is specified either according to their
natural order (see Comparable), or
according to a Comparator

Furthermore, PriorityQueues support generic data types. Therefore, if you implement Comparable in your Node class, then you can create a PriorityQueue<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 the PriorityQueue constructor. If you prefer this method, your node class does not need to contain the extra code needed when inheriting Comparable.

御弟哥哥 2024-09-03 06:39:41

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().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文