PriorityQueue(优先级队列),其本质是一个小根堆,以数组的形式储存
成员变量
//默认数组大小
private static final int DEFAULT_INITIAL_CAPACITY = 11;
//储存数据的数组
private transient Object[] queue;
//当前数组长度
private int size = 0;
//被改变次数
private transient int modCount = 0;
//数组最大长度
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
构造函数
//无参构造
public PriorityQueue() {
this(DEFAULT_INITIAL_CAPACITY, null);
}
//传入了数组长度有
public PriorityQueue(int initialCapacity) {
this(initialCapacity, null);
}
siftUpUsingComparator
从下往上调整
private void siftUpUsingComparator(int k, E x) {
while (k > 0) {
int parent = (k - 1) >>> 1;
Object e = queue[parent];
if (comparator.compare(x, (E) e) >= 0)
break;
queue[k] = e;
k = parent;
}
queue[k] = x;
}
**siftUp
下往上调整**
private void siftUp(int k, E x) {
if (comparator != null)
siftUpUsingComparator(k, x);
else
siftUpComparable(k, x);
}
siftDownUsingComparator
从上往下调整
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;
}
siftDown
上往下调整
private void siftDown(int k, E x) {
if (comparator != null)
siftDownUsingComparator(k, x);
else
siftDownComparable(k, x);
}
grow
进行扩容操作
private void grow(int minCapacity) {
int oldCapacity = queue.length;
int newCapacity = oldCapacity + ((oldCapacity < 64) ?
(oldCapacity + 2) :
(oldCapacity >> 1));
//容量小于64的时候扩容方式为2old+2扩容 大于64的时候为1.5扩容
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
queue = Arrays.copyOf(queue, newCapacity);
}
add
添加元素
public boolean add(E e) {
return offer(e);
}
offer
插入操作
public boolean offer(E e) {
if (e == null)
throw new NullPointerException();
modCount++;
int i = size;
//如果超过容量需要先进行扩容
if (i >= queue.length)
grow(i + 1);
size = i + 1;
//如果容量为0直接放入到0号下标
if (i == 0)
queue[0] = e;
else
//如果不是就需要进行调整
siftUp(i, e);
return true;
}
removeAt
通过下表删除某个元素
private E removeAt(int i) {
assert i >= 0 && i < size;
modCount++;
int s = --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) {
siftUp(i, moved);
if (queue[i] != moved)
return moved;
}
}
return null;
}
poll
删除第一个元素
public E poll() {
if (size == 0)
return null;
int s = --size;
modCount++;
E result = (E) queue[0];
E x = (E) queue[s];
queue[s] = null;
if (s != 0)
siftDown(0, x);
return result;
}
peek
只返回第一个元素,不删除
public E peek() {
if (size == 0)
return null;
return (E) queue[0];
}
clear
清空队列
public void clear() {
modCount++;
for (int i = 0; i < size; i++)
queue[i] = null;
size = 0;
}
indexOf
通过值返回下标
private int indexOf(Object o) {
if (o != null) {
for (int i = 0; i < size; i++)
if (o.equals(queue[i]))
return i;
}
return -1;
}
remove
删除某一个值
public boolean remove(Object o) {
int i = indexOf(o);
if (i == -1)
return false;
else {
removeAt(i);
return true;
}
}
contains
查找队列中是否存在某个值
public boolean contains(Object o) {
return indexOf(o) != -1;
}