privatevoidreadObject(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();
privatevoidsiftDown(int k, E x) { if (comparator != null) siftDownUsingComparator(k, x); else siftDownComparable(k, x); }
siftDownUsingComparator又调用了,集合的compare方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
privatevoidsiftDownUsingComparator(int k, E x) { inthalf= size >>> 1; while (k < half) { intchild= (k << 1) + 1; Objectc= queue[child]; intright= child + 1; if (right < size && comparator.compare((E) c, (E) queue[right]) > 0) c = queue[child = right]; if (comparator.compare(x, (E) c) <= 0) break; queue[k] = c; k = child; } queue[k] = x; }
其构造函数的参数为int和实现Comparator接口的的对象。
1 2 3 4 5 6 7 8 9
publicPriorityQueue(int initialCapacity, Comparator<? super E> comparator) { // Note: This restriction of at least one is not actually needed, // but continues for 1.5 compatibility if (initialCapacity < 1) thrownewIllegalArgumentException(); this.queue = newObject[initialCapacity]; this.comparator = comparator; }