优先队列和比较器

发布于 2024-12-12 11:34:24 字数 527 浏览 0 评论 0原文

我试图从 localNodes 获取与哈希图距离最小的节点。当我更改距离散列图中的任何值时,问题是队列不会重新排序,我认为问题是因为我正在实现比较器接口,该接口将作为内部类并强制我将散列图声明为最终的。 有什么不同的方法吗?

    final HashMap<Node, Double> distance = new HashMap<>();
    PriorityQueue<Node> localNodes = new PriorityQueue<>(10,
            new Comparator() {

                @Override
                public int compare(Object o1, Object o2) {
                    return distance.get((Node)o2).compareTo(distance.get((Node)o2) );
                }
            });

I am trying to get from localNodes the Node who has the minimum distance from the hashmap distance. the problem when am changing any value in the distance hashmap the queue is not reordered i think the problem is because i am implemting the comparator interface like that which will be as an inner class and enforce me to declare the hashmap as final.
is there any different method.

    final HashMap<Node, Double> distance = new HashMap<>();
    PriorityQueue<Node> localNodes = new PriorityQueue<>(10,
            new Comparator() {

                @Override
                public int compare(Object o1, Object o2) {
                    return distance.get((Node)o2).compareTo(distance.get((Node)o2) );
                }
            });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

爱的十字路口 2024-12-19 11:34:24

您无法更改已排序集合(例如 PriorityQueueSortedSet 并期望他们在该集合中的位置因此而改变。一方面,数据结构并不是为此而构建的。另一方面,在您的示例中,PriorityQueue不会收到您对HashMap所做的任何更改的通知,因此即使它是设计的,它也无法响应。这样做。

我不知道您如何使用此队列,但一种可能的解决方案是将节点存储在 HashMap 中,并在每次更改时创建一个新的 PriorityQueue 哈希映射。新创建的队列将具有正确的顺序。

You can't change the relative ordering of elements (in this case, by changing the distance values they map to) that are already in a sorted collection such as a PriorityQueue or SortedSet and expect their position in that collection to change as a result. For one thing, the data structures just aren't built for that. For another, in your example the PriorityQueue will not be notified of any changes you make to the HashMap so it wouldn't be able to respond to that even if it were designed to do so.

I don't know how you're using this queue, but one possible solution would be to just store the nodes in your HashMap and create a new PriorityQueue whenever you change the HashMap. The newly created queue will have the correct ordering.

只是我以为 2024-12-19 11:34:24

你不能那样做(参见科林的回答)。我可以建议一种解决方法:

创建一个如下所示的 NodeEntry 对象:

public class NodeEntry {
    private Node node;
    private Double value;
}

并使用 PriorityQueue。但是,如果您更改 NodeEntryvalue,则不会发生任何变化。当某些值发生变化时,您无法对队列重新排序。

You can't do it like that (See Colin's answer). I can suggest a workaround:

Make a NodeEntry object that looks like that:

public class NodeEntry {
    private Node node;
    private Double value;
}

And use a PriorityQueue<NodeEntry>. But if you change NodeEntry's value nothing will change. You can't reorder the queue when some value changes.

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