英文:
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<Integer, PriorityQueue<Integer>> map = new HashMap<>();
Initialization of a max heap using lambda can be like:
PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> b - a);
However, how to initialize a hash map with a max heap using lambda?
This definitly doesn't work.
Map<Integer, PriorityQueue<Integer>((a, b) -> b - a)> map = new HashMap<>();
答案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
,通过声明自己的MinHeap
和MaxHeap
类型。
MinHeap
和MaxHeap
可以继承自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<Integer, PriorityQueue<Integer>> map = new HashMap<>();
This creates a HashMap
, whose keys are integers, and whose values are PriorityQueue<Integer>
s.
A PriorityQueue<Integer>
can act like a max heap or a min heap, depending on the Comparator<Integer>
that you pass into its constructor. This means that map
above can contain both max heaps and min heaps.
map.put(1, new PriorityQueue<>(Comparator.naturalOrder())); // a min heap is associated with the key 1
map.put(2, new PriorityQueue<>(Comparator.reverseOrder())); // a max heap is associated with the key 2
Note that you should not use subtraction (i.e. (a, b) -> 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<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());
}
}
Now, a HashMap<Integer, MinHeap<Integer>>
only contains min heaps as values, and a HashMap<Integer, MaxHeap<Integer>>
only max heaps as values.
Map<Integer, MinHeap<Integer>> minHeapMap = new HashMap<>();
Map<Integer, MaxHeap<Integer>> maxHeapMap = new HashMap<>();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论