public class NODE{int x;int y;public NODE (int x, int y) {this.x = x;this.y = y;}@Overridepublic String toString() {return this.x + " " + this.y;}
}
public void PriorityQueueTemplate() {// 简单创建Queue<NODE> priorityQueue = new PriorityQueue<>();// 带比较器的创建方式
// Queue<NODE> priorityCmpQueue = new PriorityQueue<>(new Comparator<NODE>() {
// @Override
// public int compare(NODE o1, NODE o2) {
// if (o1.x != o2.x) {
// return o1.x-o2.y<0? -1:1;
// } else {
// if (o1.y < o2.y) {
// return 1;
// } else {
// return -1;
// }
// }
// }
// });Queue<NODE> priorityCmpQueue = new PriorityQueue<>((a, b)-> a.x != b.x? a.x-b.x : b.y-a.y);priorityCmpQueue.add(new NODE(2, 3));priorityCmpQueue.add(new NODE(2, 4));priorityCmpQueue.add(new NODE(4, 7));priorityCmpQueue.add(new NODE(4, 2));// 提取 q.peek()返回队首元素,不删除// q.poll()返回队首元素,删除// q.add()添加元素while (!priorityCmpQueue.isEmpty()) {System.out.println(priorityCmpQueue.poll());}
}