https://blog.csdn.net/u010675669/article/details/86503464

源码@since 1.5

类继承

  1. class PriorityQueue<E> extends AbstractQueue<E> implements java.io.Serializable

常量值

  1. private static final int DEFAULT_INITIAL_CAPACITY = 11;

变量(未搞明白)

  1. // 优先级队列表示为一个平衡的二进制堆:queue[n]的两个子队列是queue[2n+1]和queue[2(n+1)]。对于堆中的每个节点n和n的每个后代节点d, n <= d。如果队列非空,值最小的元素在队列[0]中。
  2. transient Object[] queue;
  3. private int size = 0;
  4. // 这个优先队列在结构上被修改的次数。详情请参阅AbstractList。
  5. transient int modCount = 0;
  6. private final Comparator<? super E> comparator;

构造函数

  1. public PriorityQueue() {
  2. this(DEFAULT_INITIAL_CAPACITY, null);
  3. }
  4. public PriorityQueue(int initialCapacity) {
  5. this(initialCapacity, null);
  6. }
  7. // @since 1.8
  8. public PriorityQueue(Comparator<? super E> comparator) {
  9. this(DEFAULT_INITIAL_CAPACITY, comparator);
  10. }
  11. // 实现逻辑
  12. public PriorityQueue(int initialCapacity,
  13. Comparator<? super E> comparator) {
  14. // Note: This restriction of at least one is not actually needed,
  15. // but continues for 1.5 compatibility
  16. if (initialCapacity < 1)
  17. throw new IllegalArgumentException();
  18. this.queue = new Object[initialCapacity];
  19. this.comparator = comparator;
  20. }
  21. // 使用集合
  22. public PriorityQueue(Collection<? extends E> c) {
  23. if (c instanceof SortedSet<?>) {
  24. SortedSet<? extends E> ss = (SortedSet<? extends E>) c;
  25. this.comparator = (Comparator<? super E>) ss.comparator();
  26. initElementsFromCollection(ss);
  27. }
  28. else if (c instanceof PriorityQueue<?>) {
  29. PriorityQueue<? extends E> pq = (PriorityQueue<? extends E>) c;
  30. this.comparator = (Comparator<? super E>) pq.comparator();
  31. initFromPriorityQueue(pq);
  32. }
  33. else {
  34. this.comparator = null;
  35. initFromCollection(c);
  36. }
  37. }
  38. // 使用PriorityQueue
  39. public PriorityQueue(PriorityQueue<? extends E> c) {
  40. this.comparator = (Comparator<? super E>) c.comparator();
  41. initFromPriorityQueue(c);
  42. }
  43. private void initFromPriorityQueue(PriorityQueue<? extends E> c) {
  44. if (c.getClass() == PriorityQueue.class) {
  45. this.queue = c.toArray();
  46. this.size = c.size();
  47. } else {
  48. initFromCollection(c);
  49. }
  50. }
  51. private void initFromCollection(Collection<? extends E> c) {
  52. initElementsFromCollection(c);
  53. heapify();
  54. }
  55. private void heapify() {
  56. for (int i = (size >>> 1) - 1; i >= 0; i--)
  57. siftDown(i, (E) queue[i]);
  58. }
  59. private void siftDown(int k, E x) {
  60. if (comparator != null)
  61. siftDownUsingComparator(k, x);
  62. else
  63. siftDownComparable(k, x);
  64. }
  65. private void siftDownComparable(int k, E x) {
  66. Comparable<? super E> key = (Comparable<? super E>)x;
  67. int half = size >>> 1; // loop while a non-leaf
  68. while (k < half) {
  69. int child = (k << 1) + 1; // assume left child is least
  70. Object c = queue[child];
  71. int right = child + 1;
  72. if (right < size &&
  73. ((Comparable<? super E>) c).compareTo((E) queue[right]) > 0)
  74. c = queue[child = right];
  75. if (key.compareTo((E) c) <= 0)
  76. break;
  77. queue[k] = c;
  78. k = child;
  79. }
  80. queue[k] = key;
  81. }
  82. private void siftDownUsingComparator(int k, E x) {
  83. int half = size >>> 1;
  84. while (k < half) {
  85. int child = (k << 1) + 1;
  86. Object c = queue[child];
  87. int right = child + 1;
  88. if (right < size &&
  89. comparator.compare((E) c, (E) queue[right]) > 0)
  90. c = queue[child = right];
  91. if (comparator.compare(x, (E) c) <= 0)
  92. break;
  93. queue[k] = c;
  94. k = child;
  95. }
  96. queue[k] = x;
  97. }
  98. // 使用SortedSet
  99. public PriorityQueue(SortedSet<? extends E> c) {
  100. this.comparator = (Comparator<? super E>) c.comparator();
  101. initElementsFromCollection(c);
  102. }
  103. private void initElementsFromCollection(Collection<? extends E> c) {
  104. Object[] a = c.toArray();
  105. // If c.toArray incorrectly doesn't return Object[], copy it.
  106. if (a.getClass() != Object[].class)
  107. a = Arrays.copyOf(a, a.length, Object[].class);
  108. int len = a.length;
  109. if (len == 1 || this.comparator != null)
  110. for (int i = 0; i < len; i++)
  111. if (a[i] == null)
  112. throw new NullPointerException();
  113. this.queue = a;
  114. this.size = a.length;
  115. }

常用方法