ArrayList LinkedList Vector 集合區別

1、ArrayList特點詳解

存儲結構:數組存儲,

線程安全:非安全線程

查詢速度:速度快

更新刪除:速度慢

初始化:10個數組空間,後續以5個增加

/**
 * 1、數據結構,數組存儲類型
 * 2、初始化:10個結構,後續以5個增加
 * 3、速度:set,get,add,較快,。remove,add,慢[後面數據移位]
 * 4、線程安全:不是安全線程
 */

private void testArrayList() {
    //場景一:
    ArrayList arrayList = new ArrayList();
    arrayList.add(1);
    arrayList.add(2);
    arrayList.add(3);
    arrayList.add(4);
    //源碼:
    //初始定義一個常量爲10,默認數據elementData,add時創建
    /**
     * Default initial capacity.
     * private static final int DEFAULT_CAPACITY = 10;
     * //調取add方法,創建一個elementData大小的數組
     * public boolean add(E e) {
     *      ensureCapacityInternal(size + 1);  // Increments modCount!!
     *      elementData[size++] = e;
     *      return true;
     * }*/

    /**
     * The array buffer into which the elements of the ArrayList are stored.
     * The capacity of the ArrayList is the length of this array buffer. Any
     * empty ArrayList with elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA
     * will be expanded to DEFAULT_CAPACITY when the first element is added.
     */
    //transient Object[] elementData;


    /**
     * 場景二:
     * add,remove,數組位後移動,速度較慢
     */
    Object oldValue = arrayList.remove(1);
   /* 源碼:
   public E remove(int index) {
        rangeCheck(index);//校驗數據越界
        modCount++;
        E oldValue = elementData(index);//返回被刪除數據
        int numMoved = size - index - 1;//需要被移動數組
        if (numMoved > 0)               //如果不是最後一位
            System.arraycopy(elementData, index+1, elementData, index,
                    numMoved);          //進行數據拷貝移動
        elementData[--size] = null;

        return oldValue;*/

    arrayList.add(1, "add");//源碼同上,直接移動拷貝


    /**
     * 場景三:
     * 非安全線程,多線程操作時,可能會數據操作失誤。
     */
    Object setOldValue = arrayList.set(2, "set");
    /*源碼:
    沒有進行,加鎖操作,所以不安全
    public E set(int index, E element) {
        rangeCheck(index);
        E oldValue = elementData(index);
        elementData[index] = element;
        return oldValue;
    }*/
}

2、LinkedList特點詳解

存儲結構:鏈表存儲

線程安全:非安全線程

查詢速度:速度慢

更新刪除:速度快

/**
 * 1、數據結構,雙向鏈表存儲結構
 * 3、速度:add,remove,較快,get,,較慢
 * 4、線程安全:不是安全線程
 */
private void testLinkedList() {
    /**
     * 場景一:
     *add,remove雙向鏈表插入,刪除
     */
    LinkedList linkedList = new LinkedList<>();
    linkedList.add("add");
   /*源碼:
    void linkLast(E e) {
        final LinkedList.Node<E> l = last; //上一個節點
        final LinkedList.Node<E> newNode = new LinkedList.Node<>(l, e, null); //存儲當前節點
        last = newNode;                     //更新上一個節點爲
        if (l == null)
            first = newNode;
        else
            l.next = newNode;
        size++;
        modCount++;
    }*/


    /**
     * 場景二:
     * get查詢較慢,雙向鏈表查詢
     */
    linkedList.get(1);
   /*雙向鏈表查詢源碼:
    Node<E> node(int index) {
        // assert isElementIndex(index);
        if (index < (size >> 1)) {  //鏈表長度size>>1一位,index比size小
            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;   //返回上一個節點
        }
}*/

    /**
     * 場景三:
     * 線程安全:非安全線程
     */
    linkedList.set(1, "set");
   /*源碼:無線程安全
    public E set(int index, E element) {
        checkElementIndex(index);
        Node<E> x = node(index);
        E oldVal = x.item;
        x.item = element;
        return oldVal;
    }*/
}

3、Vector數組(和ArrayList相同,區別:實現了synchronized線程安全)

存儲結構:數組存儲,

線程安全:非安全線程

查詢速度:速度快

更新刪除:速度慢

初始化:10個數組空間,後續以5個增加

/**
 * vector和arrayList完全相同
 * 所有修改元素的操作,都進
 */
public void testVector() {
    Vector vector = new Vector();
    vector.add("add");
    vector.set(1, "set");
    vector.remove(0);
    vector.get(1);
    //如下源碼,所以的增刪改查,都實現了,synchronized 加鎖的操作

  /*源碼:synchronized
    public synchronized E get(int index) {
    if (index >= elementCount)
        throw new ArrayIndexOutOfBoundsException(index);
    return elementData(index);
}

   public synchronized boolean add(E e) {
        modCount++;
        ensureCapacityHelper(elementCount + 1);
        elementData[elementCount++] = e;
        return true;
    }*/

}

面向開發過程,記錄學習之路。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章