Java PriorityQueue类是一种队列数据结构实现
它与遵循FIFO(先进先出)算法的标准队列不同。
//默认为小顶堆
PriorityQueue<Integer> minHeap = new PriorityQueue<>(k, (a,b)->a-b);
PriorityQueue<Integer> minHeap = new PriorityQueue<>(k, new Comparator<Integer>() {
@Override
public int compare(Integer a, Integer b) {
return a - b;
}
});
//大顶堆
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(k, (a,b)->b-a);
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(k, new Comparator<Integer>() {
@Override
public int compare(Integer a, Integer b) {
return b - a;
}
});
标签:大顶,Java,队列,new,PriorityQueue,Integer,小顶
From: https://www.cnblogs.com/wdnmdp/p/16754295.html