Priorityqueue 1.5
优先级队列 底层基于小根堆数组实现
PriorityQueue也是Queue的一个继承者,相比于一般的列表,它的特点便如它的名字一样,出队的时候可以按照优先级进行出队,所以不像LinkedList那样只能按照插入的顺序出队,PriorityQueue是可以根据给定的优先级顺序进行出队的。这里说的给定优先级顺序既可以是内部比较器,也可以是外部比较器。PriorityQueue内部是根据小顶堆的结构进行存储的,所谓小顶堆的意思,便是最小的元素总是在最上面,每次出队总是将堆顶元素移除,这样便能让出队变得有序,
特性
- 底层时可变数组,数组容量按需要增长
- 它属于非线程安全
- 跟名字一样,数组里保存的元素是根据小根堆进行排序的,具有优先级,当你出队时,会根据给定的优先级顺序及进行出队
- 不能储存null。原因是null无法进行比较
源码分析
继承和实现
An unbounded priority {@linkplain Queue queue} based on a priority heap.
The elements of the priority queue are ordered according to their
{@linkplain Comparable natural ordering}, or by a {@link Comparator}
provided at queue construction time, depending on which constructor is used. A priority queue does not permit {@code null} elements.
A priority queue relying on natural ordering also does not permit
insertion of non-comparable objects (doing so may result in
{@code ClassCastException}).
*基于优先级堆的无限制优先级{@linkplain Queue Queue}。
*优先级队列的元素根据其{@linkplain可比较自然排序},或按{@link Comparator}
*在队列构造时提供,具体取决于使用的构造函数。优先级队列不允许{@code null}元素。
*依赖于自然排序的优先级队列也不允许插入不可比较的对象
(这样做可能会导致{@code ClassCastException})。
public class PriorityQueue<E> extends AbstractQueue<E>
implements java.io.Serializable
- AbstractQueue 抽象类队列,它继承AbstractCollection,实现Queue接口
- Serializable接口实现可序列化
发现和ArrayDeque在继承和实现上的区别,
- Priorityqueue 不属于双端队列
- Priorityqueue没有实现cloneable接口,不支持被克隆
属性
private static final int DEFAULT_INITIAL_CAPACITY = 11;//默认分配数组大小
//优先级队列是使用平衡二叉堆表示的: 节点queue[n]的两个孩子分别为
//queue[2*n+1] 和 queue[2*(n+1)].
//对于堆中的任意元素n,其后代d满足:n<=d
//如果堆是非空的,则堆中最小值为queue[0]。
transient Object[] queue; // non-private to simplify nested class access 非私有以简化嵌套类访问
private int size = 0; //有效元素个数
private final Comparator<? super E> comparator; // 比较器
//修改次数
transient int modCount = 0; // non-private to simplify nested class access
构造器
// 无参构造,默认初始化数组大小为11,比较器为null,默认自然排序,此时元素必须实现 comparable接口
public PriorityQueue() {
this(DEFAULT_INITIAL_CAPACITY, null);
}
//给定容量构造器
public PriorityQueue(int initialCapacity) {
this(initialCapacity, null);
}
//给定比较器构造,数组长度默认值
public PriorityQueue(Comparator<? super E> comparator) {
this(DEFAULT_INITIAL_CAPACITY, comparator);
}
// 给定数组长度 给定比较器,进行构造
public PriorityQueue(int initialCapacity,
Comparator<? super E> comparator) {
// Note: This restriction of at least one is not actually needed, 注意:实际上不需要限制至少一个,
// but continues for 1.5 compatibility 但仍保持1.5兼容性
if (initialCapacity < 1)// 指定容量小于1 就会抛出异常
throw new IllegalArgumentException();
this.queue = new Object[initialCapacity];
this.comparator = comparator;
}
// 以下为从集合中构造优先级队列,以后在研究吧,因为没学到
@SuppressWarnings("unchecked")
public PriorityQueue(Collection<? extends E> c) {
if (c instanceof SortedSet<?>) {
SortedSet<? extends E> ss = (SortedSet<? extends E>) c;
this.comparator = (Comparator<? super E>) ss.comparator();
initElementsFromCollection(ss);
}
else if (c instanceof PriorityQueue<?>) {
PriorityQueue<? extends E> pq = (PriorityQueue<? extends E>) c;
this.comparator = (Comparator<? super E>) pq.comparator();
initFromPriorityQueue(pq);
}
else {
this.comparator = null;
initFromCollection(c);
}
}
public PriorityQueue(PriorityQueue<? extends E> c) {
this.comparator = (Comparator<? super E>) c.comparator();
initFromPriorityQueue(c);
}
@SuppressWarnings("unchecked")
public PriorityQueue(SortedSet<? extends E> c) {
this.comparator = (Comparator<? super E>) c.comparator();
initElementsFromCollection(c);
}
方法
增加
add(E e)
和offer(E e)
的语义相同,都是向优先队列中插入元素,只是Queue
接口规定二者对插入失败时的处理不同,前者在插入失败时抛出异常,后则则会返回false
。
//增加
public boolean add(E e) {
return offer(e);
}
public boolean offer(E e) {
if (e == null) // 如果e为空 抛出空指针异常
throw new NullPointerException();
modCount++;//修改次数+1
int i = size;//保存元素个数
if (i >= queue.length)//判断队满,是否扩容
grow(i + 1);//扩容 传进当前元素个数+1
size = i + 1;//更新size
if (i == 0) // 若队列中没有元素
queue[0] = e;
else
siftUp(i, e); // 根据小根堆调整
return true;
}
//扩容
private void grow(int minCapacity) {
int oldCapacity = queue.length;
// Double size if small; else grow by 50%
//如果当前容量小于64,即为堆高为6层内时,扩容原来的1倍+2,否则扩容原来的50%。
int newCapacity = oldCapacity + ((oldCapacity < 64) ?
(oldCapacity + 2) :
(oldCapacity >> 1));
// overflow-conscious code
//如果发现扩容后溢出
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);//调整
queue = Arrays.copyOf(queue, newCapacity);
}
//调整容量
private static int hugeCapacity(int minCapacity) {
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :// = Integer.MAX_VALUE
MAX_ARRAY_SIZE;// = Integer.MAX_VALUE - 8
}
private void siftUp(int k, E x) {
if (comparator != null)
siftUpUsingComparator(k, x);
else
siftUpComparable(k, x);
}
//调整根据自然顺序
//从下往上,比较它的父亲节点
// k: size位置,x要添加的元素
private void siftUpUsingComparator(int k, E x) {
while (k > 0) {//进入循环 k大于0
int parent = (k - 1) >>> 1;//找到父亲节点
Object e = queue[parent];//保存父节点的值
if (comparator.compare(x, (E) e) >= 0)//比较父节点的值和x
break; //如果大于等于0,说明子节点的值大于等于父节点的值,不用调整
queue[k] = e; //小于则将父节点的值覆盖子节点
k = parent; //令子节点下标更新到父节点
}
queue[k] = x; //出循环再将 此时找到的合适的位置放入值,调整完成
}
//根据自定义比较器调整
@SuppressWarnings("unchecked")
private void siftUpComparable(int k, E x) {
Comparable<? super E> key = (Comparable<? super E>) x;
while (k > 0) {
int parent = (k - 1) >>> 1;
Object e = queue[parent];
if (key.compareTo((E) e) >= 0)
break;
queue[k] = e;
k = parent;
}
queue[k] = key;
}
从下往上调整流程图
以3 5 7 9 为例 添加2
删除
remove()
和poll()
方法的语义完全相同,都是获取并删除队首元素,区别是当方法失败时前者抛出异常,后者返回null
。由于删除操作会改变队列的结构,为维护小顶堆的性质,需要进行必要的调整。
而remove(Object o) 它是Collection接口的方法。此时跟remove()
一样只是把待删节点当作头节点,但是这里会存在下标位置没有发生改变的情况,需要从下往上再次调整
//默认队头删除
@SuppressWarnings("unchecked")
public E poll() {
if (size == 0)//如果空队 return null
return null;
int s = --size; //保存最后一个元素的下标
modCount++;//修改次数+1
E result = (E) queue[0];//保存队头
E x = (E) queue[s]; // 保存最后一个元素的值
queue[s] = null;//令最后个位置为null
if (s != 0)//如果 队列只有一个元素直接返回值,否则进行调整
siftDown(0, x);
return result;
}
//此方法在*AbstractQueue*中实现
public E remove() {
E x = poll();
if (x != null)
return x;
else
throw new NoSuchElementException();
}
//删除具体的元素
public boolean remove(Object o) {
int i = indexOf(o);//找下标
if (i == -1)
return false;
else {
removeAt(i);//删除
return true;
}
}
private int indexOf(Object o) {
if (o != null) { // 前提o不等于null
for (int i = 0; i < size; i++) //for循环查找
if (o.equals(queue[i]))
return i;
}
return -1;
}
private E removeAt(int i) {
// assert i >= 0 && i < size;
modCount++;//修改次数+1
int s = --size; //在这里直接--size并 获得最后一个元素下标
if (s == i) // removed last element 如果待删的节点再最后
queue[i] = null;// 不用调整
else {//否则需要重新调整小根堆,此时需要从上往下
E moved = (E) queue[s]; //保存最后一个元素
//这里是用最后一个元素代替要移除的元素
queue[s] = null;
siftDown(i, moved); // 调整小根堆
if (queue[i] == moved) {//如果moved的位置没有发生改变
siftUp(i, moved); //继续调整从下往上,例子在下面给出
if (queue[i] != moved)
return moved;
}
}
return null;
}
private void siftDown(int k, E x) {
if (comparator != null)
siftDownUsingComparator(k, x);//自定义比较器调整
else
siftDownComparable(k, x); //自然顺序调整
}
//自然顺序调整 从上往下
// k:待删下标 x = moved 为最后一个元素 此时的size-1
private void siftDownComparable(int k, E x) {
Comparable<? super E> key = (Comparable<? super E>)x;//保存moved的值
//这里half取队列size的一半,若比half小则说明不是叶子节点
// 因为最后一个节点的序号为size-1,其父节点(size - 2) / 2或者(size - 3 ) / 2
// 所以half所在位置为第一个叶子节点
int half = size >>> 1; // loop while a non-leaf
while (k < half) {// k在父节点
int child = (k << 1) + 1; // assume left child is least假设左孩子最小
Object c = queue[child]; // c保存左孩子的值
int right = child + 1; //找到右孩子
if (right < size && // 首先要保证有右孩子
((Comparable<? super E>) c).compareTo((E) queue[right]) > 0) // 比较右孩子和左孩子 如果右孩子小,将最小c=右孩子的值
c = queue[child = right];
if (key.compareTo((E) c) <= 0) // 如果moved(size-1的值)小于左右孩子的最小值,说明此时已经是个小根堆了,直接退出
break;
queue[k] = c; //反之就是将 左右孩子最小值覆盖其父节点的值
k = child;//更新k 继续比较
}
queue[k] = key;//最后将k位置的值放入moved(临时保存旧数组最后一个元素的值)的值
}
@SuppressWarnings("unchecked")
private void siftDownUsingComparator(int k, E x) {
int half = size >>> 1;
while (k < half) {
int child = (k << 1) + 1;
Object c = queue[child];
int right = 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;
}
以下为从上往下调整的例子
,基本流程
源代码中有一步我也给出例子
if (queue[i] == moved) {//如果moved的位置没有发生改变
siftUp(i, moved); //继续调整从下往上,例子在下面给出
获取队头元素
public E peek() {
return (size == 0) ? null : (E) queue[0];
}
public E element() {
E x = peek();
if (x != null)
return x;
else
throw new NoSuchElementException();
}
应用场景
需要根据一定的规则进行优先级排序时需要用到。