如何使用Lambda在Java中初始化一个具有最大堆的HashMap

huangapple go评论94阅读模式
英文:

How to initialize a HashMap with max Heap using Lambda in Java

问题

以下是翻译好的部分:

在Java中,使用最小堆作为值初始化哈希映射的方法如下:

Map<Integer, PriorityQueue<Integer>> map = new HashMap<>();

使用Lambda表达式初始化最大堆可以像这样:

PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> b - a);

然而,如何使用Lambda表达式初始化带有最大堆的哈希映射呢?

这绝对行不通。

Map<Integer, PriorityQueue<Integer>((a, b) -> b - a)> map = new HashMap<>();
英文:

In Java, initialization of a hash map with a min heap as the value:

Map&lt;Integer, PriorityQueue&lt;Integer&gt;&gt; map = new HashMap&lt;&gt;();

Initialization of a max heap using lambda can be like:

PriorityQueue&lt;Integer&gt; pq = new PriorityQueue&lt;&gt;((a, b) -&gt; b - a);

However, how to initialize a hash map with a max heap using lambda?

This definitly doesn't work.

Map&lt;Integer, PriorityQueue&lt;Integer&gt;((a, b) -&gt; b - a)&gt; map = new HashMap&lt;&gt;();

答案1

得分: 4

这不是“用最小堆作为值初始化哈希映射”。

Map<Integer, PriorityQueue<Integer>> map = new HashMap<>();

这创建了一个HashMap,其键为整数,其值为PriorityQueue<Integer>

PriorityQueue<Integer>可以像最大堆或最小堆一样运作,这取决于传递给其构造函数的Comparator<Integer>。这意味着上面的map可以包含最大堆和最小堆。

map.put(1, new PriorityQueue<>(Comparator.naturalOrder())); // 与键1关联的是最小堆
map.put(2, new PriorityQueue<>(Comparator.reverseOrder())); // 与键2关联的是最大堆

请注意,不应使用减法(即(a, b) -> b - a)来比较整数。请参阅此帖子以获取更多信息。

清除了这些内容后,我们可以创建一个仅包含最小堆或仅包含最大堆的HashMap,通过声明自己的MinHeapMaxHeap类型。

MinHeapMaxHeap可以继承自PriorityQueue,并调用PriorityQueue的构造函数,传递适当的Comparator参数。

class MinHeap<T extends Comparable<T>> extends PriorityQueue<T> {
    public MinHeap() {
        super(Comparator.naturalOrder());
    }
}

class MaxHeap<T extends Comparable<T>> extends PriorityQueue<T> {
    public MaxHeap() {
        super(Comparator.reverseOrder());
    }
}

现在,HashMap<Integer, MinHeap<Integer>>仅包含最小堆作为值,而HashMap<Integer, MaxHeap<Integer>>仅包含最大堆作为值。

英文:

This does not "initialise of a hash map with a min heap as the value".

Map&lt;Integer, PriorityQueue&lt;Integer&gt;&gt; map = new HashMap&lt;&gt;();

This creates a HashMap, whose keys are integers, and whose values are PriorityQueue&lt;Integer&gt;s.

A PriorityQueue&lt;Integer&gt; can act like a max heap or a min heap, depending on the Comparator&lt;Integer&gt; that you pass into its constructor. This means that map above can contain both max heaps and min heaps.

map.put(1, new PriorityQueue&lt;&gt;(Comparator.naturalOrder())); // a min heap is associated with the key 1
map.put(2, new PriorityQueue&lt;&gt;(Comparator.reverseOrder())); // a max heap is associated with the key 2

Note that you should not use subtraction (i.e. (a, b) -&gt; b - a) to compare integers. See this post for more info.

With that cleared out of the way, we can make a HashMap that only contains min heaps, or only contains max heaps, by declaring our own MinHeap and MaxHeap types.

MinHeap and MaxHeap can inherit from PriorityQueue, and calling PriorityQueue's constructor with an appropriate Comparator argument.

class MinHeap&lt;T extends Comparable&lt;T&gt;&gt; extends PriorityQueue&lt;T&gt; {
    public MinHeap() {
        super(Comparator.naturalOrder());
    }
}

class MaxHeap&lt;T extends Comparable&lt;T&gt;&gt; extends PriorityQueue&lt;T&gt; {
    public MaxHeap() {
        super(Comparator.reverseOrder());
    }
}

Now, a HashMap&lt;Integer, MinHeap&lt;Integer&gt;&gt; only contains min heaps as values, and a HashMap&lt;Integer, MaxHeap&lt;Integer&gt;&gt; only max heaps as values.

Map&lt;Integer, MinHeap&lt;Integer&gt;&gt; minHeapMap = new HashMap&lt;&gt;();
Map&lt;Integer, MaxHeap&lt;Integer&gt;&gt; maxHeapMap = new HashMap&lt;&gt;();

huangapple
  • 本文由 发表于 2023年7月10日 15:07:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76651404.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定