大小堆的建立(其他类比)
1.1 Map的小堆
//a - b 小堆(前减后)
PriorityQueue<Integer> queue = new PriorityQueue<>((a,b)->map.get(a)-map.get(b));
1.2 Map的大堆
//b - a 大堆(后减前)
//idea Comparator 自动生成
PriorityQueue<Integer> queue = new PriorityQueue<>(new Comparator<Integer>() {@Overridepublic int compare(Integer a, Integer b) {return map.get(b) -map.get(a);}});PriorityQueue<Map.Entry<Integer, Integer>> priorityQueue = new PriorityQueue<>(new Comparator<Map.Entry<Integer, Integer>>() {@Overridepublic int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) {return o2.getValue().compareTo(o1.getValue());}});
1.3 一个类 分开写
// Customer 一个class 含id 7 初始化大小
Queue<Customer> customerPriorityQueue = new PriorityQueue<>(7, idComparator);
//匿名Comparator实现public static Comparator<Customer> idComparator = new Comparator<Customer>(){@Overridepublic int compare(Customer c1, Customer c2) {return (int) (c1.getId() - c2.getId());}};