队列概述

1.基于优先堆得无界队列(初始默认长度11,最大长度Integer.MAX_VALUE - 8;),容量会自动增长。
2.根据自然顺序、队列构造时提供的Comparator排序。
3.不允许插入null、不可比较元素,线程不安全。(PriorityBlockingQueue线程安全)
4.最小元素存放在队头。(如果并列最小,则随机存放)。
5.队列检索操作poll , remove , peek和element访问队列开头的元素
6.时间复杂度分析
入队和出队方法( offer , poll , remove()和add )提供O(log(n))时间; remove(Object)contains(Object)方法的线性时间;
固定时间的检索方法( peek , element和size )

一、队列基本信息

1.队列继承关系图

image.png

2.重要属性

  1. /**
  2. *指定队列默认长度(初始化,不指定队列长度时,采用此值)
  3. */
  4. private static final int DEFAULT_INITIAL_CAPACITY = 11;
  5. /**
  6. *底层存储元素数组。transient标记数组不会被序列化
  7. */
  8. transient Object[] queue; // non-private to simplify nested class access
  9. /**
  10. * The number of elements in the priority queue.
  11. * 记录队列中元素数量
  12. */
  13. private int size = 0;
  14. /**
  15. * The comparator, or null if priority queue uses elements' natural ordering.
  16. * 如果为null,则使用自然排序。(初始化时,可以指定比较器)
  17. */
  18. private final Comparator<? super E> comparator;
  19. /**
  20. * The number of times this priority queue has been
  21. * <i>structurally modified</i>. See AbstractList for gory details.
  22. * 记录数据中元素操作次数
  23. */
  24. transient int modCount = 0; // non-private to simplify nested class access
  25. /**
  26. * The maximum size of array to allocate.
  27. * Some VMs reserve some header words in an array.
  28. * Attempts to allocate larger arrays may result in
  29. * OutOfMemoryError: Requested array size exceeds VM limit
  30. * 指定队列存储的最大元素个数
  31. */
  32. private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;

二、源码分析

  1. /**
  2. * 可指定比较器的构成函数
  3. */
  4. public PriorityQueue(int initialCapacity,
  5. Comparator<? super E> comparator) {
  6. // Note: This restriction of at least one is not actually needed,
  7. // but continues for 1.5 compatibility
  8. if (initialCapacity < 1)
  9. throw new IllegalArgumentException();
  10. this.queue = new Object[initialCapacity];
  11. this.comparator = comparator;
  12. }
  13. /**
  14. *通过传递集合入参,初始化队列
  15. * 1.入参为SortedSet
  16. * 2.入参为队列
  17. * 3.入参为其他集合类型
  18. */
  19. public PriorityQueue(Collection<? extends E> c) {
  20. if (c instanceof SortedSet<?>) {
  21. SortedSet<? extends E> ss = (SortedSet<? extends E>) c;
  22. this.comparator = (Comparator<? super E>) ss.comparator();
  23. initElementsFromCollection(ss);
  24. }
  25. else if (c instanceof PriorityQueue<?>) {
  26. PriorityQueue<? extends E> pq = (PriorityQueue<? extends E>) c;
  27. this.comparator = (Comparator<? super E>) pq.comparator();
  28. initFromPriorityQueue(pq);
  29. }
  30. else {
  31. this.comparator = null;
  32. initFromCollection(c);
  33. }
  34. }
  35. /**
  36. * 通过【有序集合】进行队列元素初始化
  37. * 1.将集合处理为Object[]类型
  38. * 2.校验集合中是否存在null元素,如果存在则报错
  39. * 3.将1中数组赋值队列中数据对象
  40. */
  41. private void initElementsFromCollection(Collection<? extends E> c) {
  42. Object[] a = c.toArray();
  43. // If c.toArray incorrectly doesn't return Object[], copy it.
  44. if (a.getClass() != Object[].class)
  45. a = Arrays.copyOf(a, a.length, Object[].class);
  46. int len = a.length;
  47. if (len == 1 || this.comparator != null)
  48. for (int i = 0; i < len; i++)//队列中不允许存储null值
  49. if (a[i] == null)
  50. throw new NullPointerException();
  51. this.queue = a;
  52. this.size = a.length;
  53. }
  54. /**
  55. * Initializes queue array with elements from the given Collection.
  56. * 1.初始化底层数组
  57. * 2.对数组进行排序
  58. * @param c the collection
  59. */
  60. private void initFromCollection(Collection<? extends E> c) {
  61. initElementsFromCollection(c);
  62. heapify();
  63. }
  64. private void heapify() {
  65. for (int i = (size >>> 1) - 1; i >= 0; i--)
  66. siftDown(i, (E) queue[i]);
  67. }
  68. private void siftDown(int k, E x) {
  69. if (comparator != null)
  70. siftDownUsingComparator(k, x);
  71. else
  72. siftDownComparable(k, x);
  73. }
  74. /**
  75. *排序,利用数组实现二叉堆排序(可参考PriorityBlockingQueue队列)
  76. */
  77. private void siftDownComparable(int k, E x) {
  78. Comparable<? super E> key = (Comparable<? super E>)x;
  79. int half = size >>> 1; // loop while a non-leaf
  80. while (k < half) {
  81. int child = (k << 1) + 1; // assume left child is least
  82. Object c = queue[child];
  83. int right = child + 1;
  84. if (right < size &&
  85. ((Comparable<? super E>) c).compareTo((E) queue[right]) > 0)
  86. c = queue[child = right];
  87. if (key.compareTo((E) c) <= 0)
  88. break;
  89. queue[k] = c;
  90. k = child;
  91. }
  92. queue[k] = key;
  93. }