日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Java Review - Queue和Stack 源码解读

發布時間:2025/3/21 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java Review - Queue和Stack 源码解读 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • Pre
  • 概述
  • Queue
  • Deque
  • ArrayDeque
    • 一覽
    • 構造函數
    • 屬性
    • 方法
      • addFirst()
      • addLast()
      • pollFirst()
      • pollLast()
      • peekFirst()
      • peekLast()


Pre

Java Review - ArrayList 源碼解讀

Java Review - LinkedList源碼解讀


概述

Java中有Stack類,卻沒有叫做Queue的類,它是個接口的名字。當需要使用棧時,Java已不推薦使用Stack,而是推薦使用更高效的ArrayDeque;

既然Queue只是一個接口,當需要使用隊列時也就首選ArrayDeque了,次選LinkedList。


Queue

Queue接口繼承自Collection接口,除了最基本的Collection的方法之外,它還支持額外的insertion, extraction和inspection操作。

這里有兩組格式,共6個方法,

  • 一組是拋出異常的實現;
  • 另外一組是返回值的實現(沒有則返回null)。
–拋出異常的方法帶有返回值的方法
Insertadd(e)offer(e)
Removeremovepoll()
Examineelement()peek

Deque

  • Deque是"double ended queue", 表示雙向的隊列,英文讀作"deck".

  • Deque 繼承自 Queue接口,除了支持Queue的方法之外,還支持insert, remove和examine操作

  • 由于Deque是雙向的,所以可以對隊列的頭和尾都進行操作 . 同時也支持兩組格式,一組是拋出異常的實現;另外一組是返回值的實現(沒有則返回null)。共12個方法如下:

  • 當把Deque當做FIFO的queue來使用時,元素是從deque的尾部添加,從頭部進行刪除的; 所以deque的部分方法是和queue是等同的。如下
    -

  • Deque的含義是“double ended queue”,即雙端隊列,它既可以當作棧使用,也可以當作隊列使用。下表列出了Deque與Queue相對應的接

  • Deque與Stack對應的接口如下:

上面兩個表共定義了Deque的12個接口。

添加,刪除,取值都有兩套接口,它們功能相同,區別是對失敗情況的處理不同。

一組接口遇到失敗就會拋出異常

另一組遇到失敗會返回特殊值(false或null)。

除非某種實現對容量有限制,大多數情況下,添加操作是不會失敗的。雖然Deque的接口有12個之多,但無非就是對容器的兩端進行操作,或添加,或刪除,或查看。

ArrayDeque

一覽

ArrayDeque和LinkedList是Deque的兩個通用實現,由于官方更推薦使用AarryDeque用作棧和隊列,著重講解ArrayDeque的具體實現。

  • 從名字可以看出ArrayDeque底層通過數組實現,為了滿足可以同時在數組兩端插入或刪除元素的需求,該數組還必須是循環的,即循環數組(circular array),也就是說數組的任何一點都可能被看作起點或者終點。

  • ArrayDeque是非線程安全的(not thread-safe),當多個線程同時使用的時候,需要手動同步;

  • ArrayDeque不允許放入null元素


上圖中我們看到,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 <i>front</i> 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 offinitialCapacity >>>= 1;// Good luck allocating 2 ^ 30 elements}return initialCapacity;}

三個構造函數

  • 申請默認大小為16的數組
  • 提供需要空間大小的有參構造器:利用allocateElements申請空間
  • 利用現有集合的有參構造器:同樣利用allocateElements申請空間,再將現有集合中的元素拷貝到數組中
  • allocateElements(int numElements) :數組最小空間為8, 如果需要空間小于8,則申請數組大小為8,如果需要空間大于等于8,進行一定的容量擴大,而不只是提供需要數量的空間,防止下一次操作時又要進行擴容


    屬性

    ArrayDeque提供了兩個變量來操作數組:head 、 tail.

    • head指向隊列的頭,tail指向隊列尾的下一個位置,隊列滿的條件就是head == tail.

    • 擴容操作的執行時機:每次在向隊列中添加元素以后,不論是在頭部還是在尾部添加。

    • 擴容策略:空間是原空間的兩倍大,將原來數組中元素拷貝到新數組中,因為是循環隊列,可能出現head在tail后面的情況,拷貝到新數組時,從head指向開始拷貝,直到tail,也就是說,拷貝完成后,head指向新數組起始位置,tail指向最后一個元素的下一個位置。


    方法

    addFirst()

    /*** 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) //不允許放入nullthrow 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左邊的元素。


    addLast()

    則調用doubleCapacity()進行擴容。

    /*** Inserts the specified element at the end of this deque.** <p>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) //不允許放入nullthrow new NullPointerException();elements[tail] = e; //賦值if ( (tail = (tail + 1) & (elements.length - 1)) == head) //下標越界處理doubleCapacity();//擴容}

    addLast(E e)的作用是在Deque的尾端插入元素,也就是在tail的位置插入元素,由于tail總是指向下一個可以插入的空位,因此只需要elements[tail] = e;即可。插入完成后再檢查空間,如果空間已經用光調用doubleCapacity()進行擴容。


    pollFirst()

    public E pollFirst() {E result = elements[head];if (result == null)//null值意味著deque為空return null;elements[h] = null;//let GC workhead = (head + 1) & (elements.length - 1);//下標越界處理return result; }

    pollFirst()的作用是刪除并返回Deque首端元素,也即是head位置處的元素。

    如果容器不空,只需要直接返回elements[head]即可,當然還需要處理下標的問題。由于ArrayDeque中不允許放入null,當elements[head] == null時,意味著容器為空。


    pollLast()

    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 worktail = t;return result; }

    pollLast()的作用是刪除并返回Deque尾端元素,也即是tail位置前面的那個元素。


    peekFirst()

    public E peekFirst() {return elements[head]; // elements[head] is null if deque empty }

    peekFirst()的作用是返回但不刪除Deque首端元素,也即是head位置處的元素,直接返回elements[head]即可


    peekLast()

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

    peekLast()的作用是返回但不刪除Deque尾端元素,也即是tail位置前面的那個元素。

    總結

    以上是生活随笔為你收集整理的Java Review - Queue和Stack 源码解读的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。