【JAVA秒會技術之秒殺面試官】秒殺Java面試官——集合篇(一)

秒殺Java面試官——集合篇(一)java

1、集合的大致架構圖node

但願你們能緊緊記住下面這張框架圖,一旦面試官讓你「說說集合吧」,但願你們能立馬給他畫出來,邊畫邊逐一介紹每一個集合的特色,以及彼此的差別。重點是要從底層源代碼的角度來給面試官分析。面試

一說到底層代碼,可能不少人就頭疼了,總認爲知道和不知道對開發根本沒多大實用價值,會應用就好了。這個觀點,我暫不作評論。可是你們很慶幸的是,看到了本篇博客,博主將會帶你們,來分析一些在面試中必定會用到的集合底層實現原理。數組

2、List安全

1.ArrayList和Vector的區別網絡

第一句話:ArrayList和Vector底層都是數組實現的,初始容量都爲10;在ArrayList的底層,是經過定義一個DEFAULT_CAPACITY的常量來指定的,而Vector的底層,是直接在空參構造中,經過寫死了一個this(10)來指定的;數據結構

   (PS:標色的部分,估計學過Java的人都會說,人云亦云,可是你要是在面試中,說出了後面標的部分,雖然意思同樣,可是,你這樣一講,絕對瞬間提高了一個檔次,立馬就可以脫穎而出。後面的解析同理:色是衆人皆知的常識,色字體纔是精髓)架構

ArrayList源碼片斷:框架

/**
     * Default initial capacity.默認初始容量
     */
private static final int DEFAULT_CAPACITY = 10;=

Vector源碼片斷:函數

 

/**
     * Constructs an empty vector so that its internal data array
     * has size {@code 10} and its standard capacity increment is
     * zero.空參構造,其內部數據數組的大小爲10,而且它的標準容量增量爲零
     */
    public Vector() {
        this(10);
    }

 

   第二句話:Vector大部分方法的底層實現,都加了 synchronized關鍵字,因此Vector是線程同步的,而 ArrayList不是

Vector源碼片斷:

 

/**
     * Returns the number of components in this vector.
     */
    public synchronized int size() {
        return elementCount;
    }
    /**
     * Tests if this vector has no components.
     */
    public synchronized boolean isEmpty() {
        return elementCount == 0;
    }

第三句話:在查看API時,發現Vector有4個構造方法,比 ArrayList多了一個。而多的這個構造方法,是跟擴容有關的。ArrayList默認的擴容,在JDK1.6時,是按照新容量 =(原容量*3)/2+1來計算的,大約50%左右;而JDK1.7之後,是按照新容量 = 原容量 +(原容量 >> 1)來計算的,大約也在50%左右,因此都不是不少資料上說的就是50%,同時因爲位運算的速度比快,因此ArrayList在JDK1.7以後效率更高,也能夠看出來,;而在Vector中,默認狀況下,是100%增加的,可是咱們能夠經過比ArrayList多的那個構造方法來指定它增容的大小 

 

ArrayList源碼片斷:

 

/**
     * jdk1.6中:int newCapacity = (oldCapacity * 3)/2 + 1;
     */
    public void ensureCapacity(int minCapacity) {
     modCount++;
     int oldCapacity = elementData.length;
     if (minCapacity >oldCapacity) {
         Object oldData[] = elementData;
         int newCapacity = (oldCapacity * 3)/2 + 1;
            if (newCapacity <minCapacity)
        newCapacity = minCapacity;
            // minCapacity is usually close to size, so this is a win:
             elementData = Arrays.copyOf(elementData, newCapacity);
     }
    }
/**
     * jdk1.7以後:int newCapacity = oldCapacity + (oldCapacity >> 1);
     */
    private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity =elementData.length;
        int newCapacity =oldCapacity + (oldCapacity >> 1);
        if (newCapacity -minCapacity < 0)
            newCapacity = minCapacity;
        if (newCapacity -MAX_ARRAY_SIZE > 0)
            newCapacity = hugeCapacity(minCapacity);
        // minCapacity is usually close to size, so this is a win:
        elementData = Arrays.copyOf(elementData,newCapacity);
    }



Vector源碼片斷:

 

 

/**
     * Vector中int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
     */  
    private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity =elementData.length;
        int newCapacity =oldCapacity + ((capacityIncrement > 0) ?
                                         capacityIncrement :oldCapacity);
        if (newCapacity -minCapacity < 0)
            newCapacity = minCapacity;
        if (newCapacity -MAX_ARRAY_SIZE > 0)
            newCapacity = hugeCapacity(minCapacity);
        elementData = Arrays.copyOf(elementData,newCapacity);
    }


2.ArrayList與LinkedList

 

第一句話:ArrayList是實現了基於動態數組的數據結構,LinkedList基於鏈表的數據結構,它繼承於AbstractSequentialList的雙向鏈表,因爲AbstractSequentialList 實現了get(i)、set()、add() 和 remove()這些骨幹性函數,這也下降了List接口的複雜程度

LinkedList源碼片斷:

 

public class LinkedList<E>
    extends AbstractSequentialList<E>
    implements List<E>, Deque<E>, Cloneable, java.io.Serializable

 

第二句話:ArrayList與LinkedList都是否是同步的。若是多個線程同時訪問一個連接列表,而其中至少一個線程從結構上修改了該列表,則它必須保持外部同步。同步的方法就是使用Collections.synchronizedList(Collection<T> c)來「包裝」該列表。

 

static <T> Collection<T>  synchronizedCollection(Collection<T> c) 
返回指定 collection 支持的同步(線程安全的)

 

第三句話:對於隨機訪問get和set,ArrayList絕對優於LinkedList,由於從源碼能夠看出,ArrayList想要get(int index)元素時,直接返回index位置上的元素;而LinkedList須要經過for循環進行查找,雖然LinkedList已經在查找方法上作了優化,好比index < size / 2,則從左邊開始查找,反之從右邊開始查找,可是仍是比ArrayList(隨機查找)要慢。

ArrayList源碼片斷:

 

public E get(int index) {
        rangeCheck(index); 
        checkForComodification();
        return ArrayList.this.elementData(offset +index); //直接返回索引
    }

 

 

LinkedList源碼片斷:

 

public E get(int index) {
        checkElementIndex(index);
        return node(index).item; //get()方法會調用node()方法,
}
//node()方法須要循環遍歷查找索引;
    Node<E> node(int index) {
        // assert isElementIndex(index);
        if (index < (size >> 1)) {  //加速機制
            Node<E> x = first;
            for (int i = 0;i < index; i++)
                x = x.next;
            return x;
        } else {
            Node<E> x = last;
            for (int i =size - 1; i > index; i--)
                x = x.prev;
            return x;
        }
}



第四句話從源碼來看,ArrayList想要在指定位置插入或刪除元素時,主要耗時的是System.arraycopy動做,會移動index後面全部的元素;LinkedList主耗時的是要先經過for循環找到index,而後直接插入或刪除。這就致使了二者並不是必定誰快誰慢! 

 

具體狀況,可參看以下測試數據(數據來源於網絡):

 

#在index=1000出插入結果:
  array time:4
  linked time:240
  array insert time:20
  linked insert time:18
#在index=5000處插入結果: 
  array time:4
  linked time:229
  array insert time:13
  linked insert time:90
#在index=9000處插入結果:
  array time:4
  linked time:237
  array insert time:7
  linked insert time:92

    因此,不是不少資料中說得那樣簡單:覺得ArrayList查詢快,增刪慢,由於它是集合實現的,要改角標;而LinkedList是鏈表實現的,因此查詢慢,增刪快!!
         結論:當插入的數據量很小時,二者區別不太大;當插入的數據量大時,大約在容量的1/10以前,LinkedList會優於ArrayList;在其後就徹底劣於ArrayList,且越靠近後面越差。因此我的以爲,通常首選用ArrayList,由於LinkedList還能夠實現棧、隊列以及雙端隊列等數據結構。

相關文章
相關標籤/搜索