Java中有Stack类,却没有叫做Queue的类,它是个接口的名字。当需要使用栈时,Java已不推荐使用Stack,而是推荐使用更高效的ArrayDeque;
既然Queue只是一个接口,当需要使用队列时也就首选ArrayDeque了,次选LinkedList。
Queue接口继承自Collection接口,除了最基本的Collection的方法之外,它还支持额外的insertion, extraction和inspection操作。
这里有两组格式,共6个方法,
– | 抛出异常的方法 | 带有返回值的方法 |
---|---|---|
Insert | add(e) | offer(e) |
Remove | remove | poll() |
Examine | element() | peek |
Deque
当做FIFO的queue
来使用时,元素是从deque的尾部添加,从头部进行删除的; 所以deque的部分方法是和queue是等同的。如下上面两个表共定义了Deque的12个接口。
添加,删除,取值都有两套接口,它们功能相同,区别是对失败情况的处理不同。
一组接口遇到失败就会抛出异常 另一组遇到失败会返回特殊值(false或null)。 除非某种实现对容量有限制,大多数情况下,添加操作是不会失败的。虽然Deque的接口有12个之多,但无非就是对容器的两端进行操作,或添加,或删除,或查看。
ArrayDeque和LinkedList是Deque的两个通用实现,由于官方更推荐使用AarryDeque用作栈和队列,着重讲解ArrayDeque的具体实现。
上图中我们看到,head指向首端第一个有效元素,tail指向尾端第一个可以插入元素的空位。因为是循环数组,所以head不一定总等于0,tail也不一定总是比head大.
/**
* Constructs an empty array deque with an initial capacity
* sufficient to hold 16 elements.
*/
public ArrayDeque() {
elements = new Object[16];
}
/**
* Constructs an empty array deque with an initial capacity
* sufficient to hold the specified number of elements.
*
* @param numElements lower bound on initial capacity of the deque
*/
public ArrayDeque(int numElements) {
allocateElements(numElements);
}
/**
* Constructs a deque containing the elements of the specified
* collection, in the order they are returned by the collection's
* iterator. (The first element returned by the collection's
* iterator becomes the first element, or front of the
* deque.)
*
* @param c the collection whose elements are to be placed into the deque
* @throws NullPointerException if the specified collection is null
*/
public ArrayDeque(Collection<? extends E> c) {
allocateElements(c.size());
addAll(c);
}
/**
* Allocates empty array to hold the given number of elements.
*
* @param numElements the number of elements to hold
*/
private void allocateElements(int numElements) {
elements = new Object[calculateSize(numElements)];
}
/**
* The minimum capacity that we'll use for a newly created deque.
* Must be a power of 2.
*/
private static final int MIN_INITIAL_CAPACITY = 8;
// ****** Array allocation and resizing utilities ******
private static int calculateSize(int numElements) {
int initialCapacity = MIN_INITIAL_CAPACITY;
// Find the best power of two to hold elements.
// Tests "<=" because arrays aren't kept full.
if (numElements >= initialCapacity) {
initialCapacity = numElements;
initialCapacity |= (initialCapacity >>> 1);
initialCapacity |= (initialCapacity >>> 2);
initialCapacity |= (initialCapacity >>> 4);
initialCapacity |= (initialCapacity >>> 8);
initialCapacity |= (initialCapacity >>> 16);
initialCapacity++;
if (initialCapacity < 0) // Too many elements, must back off
initialCapacity >>>= 1;// Good luck allocating 2 ^ 30 elements
}
return initialCapacity;
}
三个构造函数
allocateElements(int numElements)
:数组最小空间为8, 如果需要空间小于8,则申请数组大小为8,如果需要空间大于等于8,进行一定的容量扩大,而不只是提供需要数量的空间,防止下一次操作时又要进行扩容
ArrayDeque提供了两个变量来操作数组:head 、 tail.
/**
* Inserts the specified element at the front of this deque.
*
* @param e the element to add
* @throws NullPointerException if the specified element is null
*/
public void addFirst(E e) {
if (e == null) //不允许放入null
throw new NullPointerException();
elements[head = (head - 1) & (elements.length - 1)] = e; //2.下标是否越界
if (head == tail)//1.空间是否够用
doubleCapacity();//扩容
}
addFirst(E e)的作用是在Deque的首端插入元素,也就是在head的前面插入元素,在空间足够且下标没有越界的情况下,只需要将elements–head = e即可 。
上述代码我们看到,空间问题是在插入之后解决的,因为tail总是指向下一个可插入的空位,也就意味着elements数组至少有一个空位,所以插入元素的时候不用考虑空间问题。
下标越界的处理 ,head = (head - 1) & (elements.length - 1)
就可以了,这段代码相当于取余,同时解决了head为负值的情况。
因为elements.length必需是2的指数倍,elements - 1就是二进制低位全1,跟head - 1相与之后就起到了取模的作用,如果head - 1为负数(其实只可能是-1),则相当于对其取相对于elements.length的补码。
接下来看扩容的逻辑
/**
* Doubles the capacity of this deque. Call only when full, i.e.,
* when head and tail have wrapped around to become equal.
*/
private void doubleCapacity() {
assert head == tail;
int p = head;
int n = elements.length;
int r = n - p; // number of elements to the right of p .head右边元素的个数
int newCapacity = n << 1; //原空间的2倍
if (newCapacity < 0)
throw new IllegalStateException("Sorry, deque too big");
Object[] a = new Object[newCapacity];
System.arraycopy(elements, p, a, 0, r);//复制右半部分,对应下图中绿色部分
System.arraycopy(elements, 0, a, r, p);//复制左半部分,对应下图中灰色部分
elements = a;
head = 0;
tail = n;
}
其逻辑是申请一个更大的数组(原数组的两倍),然后将原数组复制过去。
图中我们看到,复制分两次进行,第一次复制head右边的元素,第二次复制head左边的元素。
则调用doubleCapacity()进行扩容。
/**
* Inserts the specified element at the end of this deque.
*
* This method is equivalent to {@link #add}.
*
* @param e the element to add
* @throws NullPointerException if the specified element is null
*/
public void addLast(E e) {
if (e == null) //不允许放入null
throw new NullPointerException();
elements[tail] = e; //赋值
if ( (tail = (tail + 1) & (elements.length - 1)) == head) //下标越界处理
doubleCapacity();//扩容
}
addLast(E e)的作用是在Deque的尾端插入元素,也就是在tail的位置插入元素,由于tail总是指向下一个可以插入的空位,因此只需要elementstail = e;即可。插入完成后再检查空间,如果空间已经用光调用doubleCapacity()进行扩容。
public E pollFirst() {
E result = elements[head];
if (result == null)//null值意味着deque为空
return null;
elements[h] = null;//let GC work
head = (head + 1) & (elements.length - 1);//下标越界处理
return result;
}
pollFirst()的作用是删除并返回Deque首端元素,也即是head位置处的元素。
如果容器不空,只需要直接返回elementshead即可,当然还需要处理下标的问题。由于ArrayDeque中不允许放入null,当elementshead == null时,意味着容器为空。
public E pollLast() {
int t = (tail - 1) & (elements.length - 1);//tail的上一个位置是最后一个元素
E result = elements[t];
if (result == null)//null值意味着deque为空
return null;
elements[t] = null;//let GC work
tail = t;
return result;
}
pollLast()的作用是删除并返回Deque尾端元素,也即是tail位置前面的那个元素。
public E peekFirst() {
return elements[head]; // elements[head] is null if deque empty
}
peekFirst()的作用是返回但不删除Deque首端元素,也即是head位置处的元素,直接返回elementshead即可
public E peekLast() {
return elements[(tail - 1) & (elements.length - 1)];
}
peekLast()的作用是返回但不删除Deque尾端元素,也即是tail位置前面的那个元素。