用lambda定义pair排序逻辑
PriorityQueue<Pair<Integer,Integer>> q =
new PriorityQueue<Pair<Integer,Integer>>((a,b) -> a.getValue() - b.getValue());
这里(a,b) -> a.getValue() - b.getValue() 如果的逻辑是 返回值>0 则a大,否则b大
优先队列默认是小顶堆,最小元素在堆顶
如果要改变默认排序方式:
PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> b - a);
本文详细解释了如何使用lambda表达式重定义`PriorityQueue`的排序逻辑,从默认的值比较到根据需要实现的大于或小于操作。特别关注了如何在Java中调整`Pair`对象的优先级比较规则。
484

被折叠的 条评论
为什么被折叠?



