java.util.PriorityQueue#readObject分析 :
[code]// java.util.PriorityQueue#readObjectprivate void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { // Read in size, and any hidden stuff s.defaultReadObject(); // Read in (and discard) array length s.readInt(); queue = new Object[size]; // Read in all elements. for (int i = 0; i < size; i++) queue = s.readObject(); // Elements are guaranteed to be in "proper order", but the // spec has never explained what that might be. heapify(); }// java.util.PriorityQueue#heapify恢复二叉堆状态private void heapify() { // size >>> 1个非叶子节点需要调整 // 假如最后一个叶子结点的位置为n,它的父节点的位置为n>>>1,从父节点的位置往前数全部是非叶子节点 for (int i = (size >>> 1) - 1; i >= 0; i--) // 调整每个非叶子节点 siftDown(i, (E) queue); }private void siftDown(int k, E x) { // 优先队列需要比较节点的大小,comparator是比较器 if (comparator != null) siftDownUsingComparator(k, x); else siftDownComparable(k, x); }// 利用提前设置的比较器private void siftDownUsingComparator(int k, E x) { int half = size >>> 1; while (k < half) { int child = (k 0) c = queue[child = right]; // 触发org.apache.commons.collections4.comparators.TransformingComparator#compare if (comparator.compare(x, (E) c)