使用Hashmap条目中的PriorityQueue创建Minheap

发布于 2025-02-06 15:23:51 字数 684 浏览 2 评论 0原文

我正在使用PriortityQueue创建一个 min-heap ,从hashmap中填充。其中条目 MAP的对象是Integer& 整数

我正在借助比较器实现它。使用 value hashmap进行比较。

我的问题是 - 如何对hashmap type value 进行比较integer

PriorityQueue<Map.Entry<Integer, Integer>> minHeap = new PriorityQueue<>(new Comparator<Map.Entry<Integer, Integer>>() {
    @Override
    public int compare(Entry<Integer, Integer> o1, Entry<Integer, Integer> o2) {
        return o1.getValue() - o2.getValue();
    }
});

I am creating a Min-Heap using PriorityQueue populated from a HashMap. Where Entry objects of the Map are pairs of Integer & Integer.

And I'm implementing it with a help of Comparator. To use Value of HashMap for comparison.

My question is - How to make comparison of HashMap entries on Value of type Integer?

PriorityQueue<Map.Entry<Integer, Integer>> minHeap = new PriorityQueue<>(new Comparator<Map.Entry<Integer, Integer>>() {
    @Override
    public int compare(Entry<Integer, Integer> o1, Entry<Integer, Integer> o2) {
        return o1.getValue() - o2.getValue();
    }
});

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

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

发布评论

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

评论(2

乖乖 2025-02-13 15:23:51

您可以首先比较值,然后将密钥类似地进行比较:

final var queue = new PriorityQueue<>(
        Map.Entry.<Integer, Integer>comparingByValue()
                .thenComparing(Map.Entry.comparingByKey()));

You could combine the comparison by first comparing the value and then the key like so:

final var queue = new PriorityQueue<>(
        Map.Entry.<Integer, Integer>comparingByValue()
                .thenComparing(Map.Entry.comparingByKey()));
一人独醉 2025-02-13 15:23:51

定义比较器

使用匿名类定义比较器不是一个好方法。

由于 Java 8 在8年前发布了,因此在这种情况下,我们有另一种选择,甚至是一堆替代方案。

简单明了的lambda表达式:

Comparator<Map.Entry<Integer, Integer>> byValueAsc =
    (entry1, entry2) -> Integer.compare(entry1.getValue(), entry2.getValue());

注: o1.getValue()会导致结果不正确。 integer.compare() - 是比较整数值的正确方法。

java 8静态方法比较()来自比较器接口:

Comparator<Map.Entry<Integer, Integer>> byValueAsc =
    Comparator.comparingInt(Map.Entry::getValue);

最合适的选项选项。 >从输入接口:

PriorityQueue<Map.Entry<Integer, Integer>> minHeap = 
    new PriorityQueue<>(Map.Entry.comparingByValue());

填充队列

queue 使用 entries map ,您可以使用循环的普通

Map<Integer, Integer> map = new HashMap<>();
for (Map.Entry<Integer, Integer> entry: map.entrySet()) {
    minHeap.add(entry);
}

或使用collection> Collection> Collection> COADER接口条目集

PriorityQueue<Map.Entry<Integer, Integer>> minHeap = new PriorityQueue<>(Map.Entry.comparingByValue());

map.entrySet().forEach(minHeap::add);

Defining a Comparator

Defining a Comparator using an anonymous class isn't a good approach.

Since Java 8 which was released more than 8 years ago we have an alternative, or even a bunch of alternatives like in this case.

A simple and straightforward lambda expression:

Comparator<Map.Entry<Integer, Integer>> byValueAsc =
    (entry1, entry2) -> Integer.compare(entry1.getValue(), entry2.getValue());

Note: o1.getValue() - o2.getValue() - might cause overflow of int value, which will lead to incorrect results. Integer.compare() - is the proper way to compare integer values.

Java 8 static method comparingInt() from the Comparator interface:

Comparator<Map.Entry<Integer, Integer>> byValueAsc =
    Comparator.comparingInt(Map.Entry::getValue);

And the most suitable option it this case comparingByValue() from the Entry interface :

PriorityQueue<Map.Entry<Integer, Integer>> minHeap = 
    new PriorityQueue<>(Map.Entry.comparingByValue());

Populating the Queue

To populate the queue with entries from a map, you can use a plain for loop:

Map<Integer, Integer> map = new HashMap<>();
for (Map.Entry<Integer, Integer> entry: map.entrySet()) {
    minHeap.add(entry);
}

Or make use of method forEach() from Collection interface invoked on the entry set:

PriorityQueue<Map.Entry<Integer, Integer>> minHeap = new PriorityQueue<>(Map.Entry.comparingByValue());

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