天天看點

Java 容器源碼分析之 Deque 與 ArrayDeque

Queue 也是 Java 集合架構中定義的一種接口,直接繼承自 Collection 接口。除了基本的 Collection 接口規定測操作外,Queue 接口還定義一組針對隊列的特殊操作。通常來說,Queue 是按照先進先出(FIFO)的方式來管理其中的元素的,但是優先隊列是一個例外。

Deque 接口繼承自 Queue接口,但 Deque 支援同時從兩端添加或移除元素,是以又被成為雙端隊列。鑒于此,Deque 接口的實作可以被當作 FIFO隊列使用,也可以當作LIFO隊列(棧)來使用。官方也是推薦使用 Deque 的實作來替代 Stack。

ArrayDeque 是 Deque 接口的一種具體實作,是依賴于可變數組來實作的。ArrayDeque 沒有容量限制,可根據需求自動進行擴容。ArrayDeque不支援值為 null 的元素。

下面基于JDK 8中的實作對 ArrayDeque 加以分析。

方法概覽

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
      
public interface Queue<E> extends Collection<E> {
    //向隊列中插入一個元素,并傳回true
    //如果隊列已滿,抛出IllegalStateException異常
    boolean add(E e);

    //向隊列中插入一個元素,并傳回true
    //如果隊列已滿,傳回false
    boolean offer(E e);

    //取出隊列頭部的元素,并從隊列中移除
    //隊列為空,抛出NoSuchElementException異常
    E remove();

    //取出隊列頭部的元素,并從隊列中移除
    //隊列為空,傳回null
    E poll();

    //取出隊列頭部的元素,但并不移除
    //如果隊列為空,抛出NoSuchElementException異常
    E element();

    //取出隊列頭部的元素,但并不移除
    //隊列為空,傳回null
    E peek();
}
      

Deque 提供了雙端的插入與移除操作,如下表:

First Element (Head) Last Element (Tail)
Throws exception Special value
Insert addFirst(e) offerFirst(e) addLast(e) offerLast(e)
Remove removeFirst() pollFirst() removeLast() pollLast()
Examine getFirst() peekFirst() getLast() peekLast()

Deque 和 Queue 方法的的對應關系如下:

Queue Method Equivalent Deque Method
add(e)
offer(e)
remove()
poll()
element()
peek()

Deque 和 Stack 方法的對應關系如下:

Stack Method
push(e)
pop()

ArrayList 實作了 Deque 接口中的所有方法。因為 ArrayList 會根據需求自動擴充容量,因而在插入元素的時候不會抛出IllegalStateException異常。

底層結構

1
2
3
4
5
6
7
8
      
//用數組存儲元素
transient Object[] elements; // non-private to simplify nested class access
//頭部元素的索引
transient int head;
//尾部下一個将要被加入的元素的索引
transient int tail;
//最小容量,必須為2的幂次方
private static final int MIN_INITIAL_CAPACITY = 8;
      

在 ArrayDeque 底部是使用數組存儲元素,同時還使用了兩個索引來表征目前數組的狀态,分别是 head 和 tail。head 是頭部元素的索引,但注意 tail 不是尾部元素的索引,而是尾部元素的下一位,即下一個将要被加入的元素的索引。

初始化

ArrayDeque 提供了三個構造方法,分别是預設容量,指定容量及依據給定的集合中的元素進行建立。預設容量為16。

1
2
3
4
5
6
7
8
9
10
11
12
      
public ArrayDeque() {
    elements = new Object[16];
}

public ArrayDeque(int numElements) {
    allocateElements(numElements);
}

public ArrayDeque(Collection<? extends E> c) {
    allocateElements(c.size());
    addAll(c);
}
      

ArrayDeque 對數組的大小(即隊列的容量)有特殊的要求,必須是 2^n。通過 

allocateElements

方法計算初始容量:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
      
private void allocateElements(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
    }
    elements = new Object[initialCapacity];
}
      

>>>

是無符号右移操作,

|

是位或操作,經過五次右移和位或操作可以保證得到大小為2^k-1的數。看一下這個例子:

1
2
3
4
      
0 0 0 0 1 ? ? ? ? ?     //n
0 0 0 0 1 1 ? ? ? ?     //n |= n >>> 1;
0 0 0 0 1 1 1 1 ? ?     //n |= n >>> 2;
0 0 0 0 1 1 1 1 1 1     //n |= n >>> 4;
      

在進行5次位移操作和位或操作後就可以得到2^k-1,最後加1即可。這個實作還是很巧妙的。

添加元素

向末尾添加元素:

1
2
3
4
5
6
7
8
9
10
11
      
public void addLast(E e) {
        if (e == null)
            throw new NullPointerException();
        //tail 中儲存的是即将加入末尾的元素的索引
        elements[tail] = e;
        //tail 向後移動一位
        //把數組當作環形的,越界後到0索引
        if ( (tail = (tail + 1) & (elements.length - 1)) == head)
            //tail 和 head相遇,空間用盡,需要擴容
            doubleCapacity();
    }
      

這段代碼中,

(tail = (tail + 1) & (elements.length - 1)) == head

這句有點難以了解。其實,在 ArrayDeque 中數組是當作環形來使用的,索引0看作是緊挨着索引(length-1)之後的。參考下面的圖檔:

Java 容器源碼分析之 Deque 與 ArrayDeque

那麼為什麼

(tail + 1) & (elements.length - 1)

就能保證按照環形取得正确的下一個索引值呢?這就和前面說到的 ArrayDeque 對容量的特殊要求有關了。下面對其正确性加以驗證:

1
2
3
4
5
      
length = 2^n,二進制表示為: 第 n 位為1,低位 (n-1位) 全為0 
length - 1 = 2^n-1,二進制表示為:低位(n-1位)全為1

如果 tail + 1 <= length - 1,則位與後低 (n-1) 位保持不變,高位全為0
如果 tail + 1 = length,則位與後低 n 全為0,高位也全為0,結果為 0
      

可見,在容量保證為 2^n 的情況下,僅僅通過位與操作就可以完成環形索引的計算,而不需要進行邊界的判斷,在實作上更為高效。

向頭部添加元素的代碼如下:

1
2
3
4
5
6
7
      
public void addFirst(E e) {
    if (e == null) //不支援值為null的元素
        throw new NullPointerException();
    elements[head = (head - 1) & (elements.length - 1)] = e;
    if (head == tail)
        doubleCapacity();
}
      

其它的諸如add,offer,offerFirst,offerLast等方法都是基于上面這兩個方法實作的,不再贅述。

擴容

在每次添加元素後,如果頭索引和尾部索引相遇,則說明數組空間已滿,需要進行擴容操作。 ArrayDeque 每次擴容都會在原有的容量上翻倍,這也是對容量必須是2的幂次方的保證。

Java 容器源碼分析之 Deque 與 ArrayDeque
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
      
private void doubleCapacity() {
    assert head == tail; //擴容時頭部索引和尾部索引肯定相等
    int p = head;
    int n = elements.length;
    //頭部索引到數組末端(length-1處)共有多少元素
    int r = n - p; // number of elements to the right of p
    //容量翻倍
    int newCapacity = n << 1;
    //容量過大,溢出了
    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;
}
      

移除元素

ArrayDeque支援從頭尾兩端移除元素,remove方法是通過poll來實作的。因為是基于數組的,在了解了環的原理後這段代碼就比較容易了解了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
      
public E pollFirst() {
    int h = head;
    @SuppressWarnings("unchecked")
    E result = (E) elements[h];
    // Element is null if deque empty
    if (result == null)
        return null;
    elements[h] = null;     // Must null out slot
    head = (h + 1) & (elements.length - 1);
    return result;
}

public E pollLast() {
    int t = (tail - 1) & (elements.length - 1);
    @SuppressWarnings("unchecked")
    E result = (E) elements[t];
    if (result == null)
        return null;
    elements[t] = null;
    tail = t;
    return result;
}
      

擷取隊頭和隊尾的元素

1
2
3
4
5
6
7
8
9
10
      
@SuppressWarnings("unchecked")
public E peekFirst() {
    // elements[head] is null if deque empty
    return (E) elements[head];
}

@SuppressWarnings("unchecked")
public E peekLast() {
    return (E) elements[(tail - 1) & (elements.length - 1)];
}
      

疊代器

ArrayDeque 在疊代是檢查并發修改并沒有使用類似于 ArrayList 等容器中使用的 modCount,而是通過尾部索引的來确定的。具體參考 next 方法中的注釋。但是這樣不一定能保證檢測到所有的并發修改情況,加入先移除了尾部元素,又添加了一個尾部元素,這種情況下疊代器是沒法檢測出來的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
      
private class DeqIterator implements Iterator<E> {
    /**
     * Index of element to be returned by subsequent call to next.
     */
    private int cursor = head;

    /**
     * Tail recorded at construction (also in remove), to stop
     * iterator and also to check for comodification.
     */
    private int fence = tail;

    /**
     * Index of element returned by most recent call to next.
     * Reset to -1 if element is deleted by a call to remove.
     */
    private int lastRet = -1;

    public boolean hasNext() {
        return cursor != fence;
    }

    public E next() {
        if (cursor == fence)
            throw new NoSuchElementException();
        @SuppressWarnings("unchecked")
        E result = (E) elements[cursor];
        // This check doesn't catch all possible comodifications,
        // but does catch the ones that corrupt traversal
        // 如果移除了尾部元素,會導緻tail != fence
        // 如果移除了頭部元素,會導緻 result == null
        if (tail != fence || result == null)
            throw new ConcurrentModificationException();
        lastRet = cursor;
        cursor = (cursor + 1) & (elements.length - 1);
        return result;
    }

    public void remove() {
        if (lastRet < 0)
            throw new IllegalStateException();
        if (delete(lastRet)) { // if left-shifted, undo increment in next()
            cursor = (cursor - 1) & (elements.length - 1);
            fence = tail;
        }
        lastRet = -1;
    }

    public void forEachRemaining(Consumer<? super E> action) {
        Objects.requireNonNull(action);
        Object[] a = elements;
        int m = a.length - 1, f = fence, i = cursor;
        cursor = f;
        while (i != f) {
            @SuppressWarnings("unchecked") E e = (E)a[i];
            i = (i + 1) & m;
            if (e == null)
                throw new ConcurrentModificationException();
            action.accept(e);
        }
    }
}
      

除了 DeqIterator,還有一個反向的疊代器 DescendingIterator,順序和 DeqIterator 相反。

小結

ArrayDeque 是 Deque 接口的一種具體實作,是依賴于可變數組來實作的。ArrayDeque 沒有容量限制,可根據需求自動進行擴容。ArrayDeque 可以作為棧來使用,效率要高于 Stack;ArrayDeque 也可以作為隊列來使用,效率相較于基于雙向連結清單的 LinkedList 也要更好一些。注意,ArrayDeque 不支援為 null 的元素。

熬夜不易,點選請老王喝杯烈酒!!!!!!!