【源碼閱讀】Java集合之一 - ArrayList源碼深度解讀

Java 源碼閱讀的第一步是Collection框架源碼,這也是面試基礎中的基礎; 針對Collection的源碼閱讀寫一個系列的文章,從ArrayList開始第一篇。 ---@pdaihtml

JDK版本

JDK 1.8.0_110java

概述總結

  • ArrayList底層是經過數組實現的;其中capacity表示底層數組的長度,而ArrayList長度由size表示;
  • ArrayList容許存放null元素,也能夠查找null所在的index, 好比indexOf(), lastIndexOf()方法;
  • ArrayList默認的capacity是10,當向容器中添加元素時,若是容量不足,容器會自動增大底層數組的大小;
  • ArrayList添加或者刪除指定index元素時,須要拷貝 index到size下標的數據,因此ArrayList中數據量較大時,作指定位置的增刪操做,開銷很大, 時間開銷跟插入或刪除位置有關;
  • ArrayList在元素刪除時須要將該元素所在位置的值設爲null,以方便GC按需回收;
  • ArrayList數據的長度受制於底層Object[]的長度size,而size是int型的,因此受制於int能表示的最大長度,這裏size最大是Integer.MAX_INT - 8;
  • ArrayList沒有實現同步(synchronized),若是須要多個線程併發訪問,用戶能夠手動同步,也可以使用Vector替代; 有些場景也能夠考慮使用List list = Collections.synchronizedList(new ArrayList(...))
  • ArrayList也採用了快速失敗的機制,經過記錄modCount參數來實現。在面對併發的修改時,迭代器很快就會徹底失敗,而不是冒着在未來某個不肯定時間發生任意不肯定行爲的風險;
  • ArrayList底層的數據類型是Object[], Java泛型只是編譯器提供的語法糖,因此這裏的數組是一個Object數組,以便可以容納任何類型的對象;

類關係圖

ArrayList實現的接口和繼承的類以下:面試

public class ArrayList<E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
}

以下關係圖中須要考慮幾個問題:數組

  • AbstractList 已經實現了List 的接口,那爲何ArrayList 還要實現List 接口呢? 主要仍是爲了支持Java的泛型。
  • RandomAccess和Cloneable接口都沒有具體要實現的方法,爲啥要還要實現它們?由於它們是標記型接口。
  • Iterable接口在ArrayList中實現有什麼注意點? 須要的,interator()方法同時也要支持fail-fast機制,具體類爲private的Itr內部類。

類的實現

底層數據結構

底層是經過Object[] elementData實現,修飾類型是非私有的,可讓派生類直接訪問。在默認初始化時elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA, 當第一次添加元素時,容量會初始化爲DEFAULT_CAPACITY(10)。數據結構

/**
     * Default initial capacity.
     */
    private static final int DEFAULT_CAPACITY = 10;

    /**
     * Shared empty array instance used for empty instances.
     */
    private static final Object[] EMPTY_ELEMENTDATA = {};

    /**
     * Shared empty array instance used for default sized empty instances. We
     * distinguish this from EMPTY_ELEMENTDATA to know how much to inflate when
     * first element is added.
     */
    private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
    
    /**
     * 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; // non-private to simplify nested class access

    /**
     * The size of the ArrayList (the number of elements it contains).
     *
     * @serial
     */
    private int size;

構造函數

默認初始化elementData指向DEFAULTCAPACITY_EMPTY_ELEMENTDATA數組;併發

/**
     * Constructs an empty list with the specified initial capacity.
     *
     * @param  initialCapacity  the initial capacity of the list
     * @throws IllegalArgumentException if the specified initial capacity
     *         is negative
     */
    public ArrayList(int initialCapacity) {
        if (initialCapacity > 0) {
            this.elementData = new Object[initialCapacity];
        } else if (initialCapacity == 0) {
            this.elementData = EMPTY_ELEMENTDATA;
        } else {
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        }
    }

    /**
     * Constructs an empty list with an initial capacity of ten.
     */
    public ArrayList() {
        this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
    }

    /**
     * Constructs a list containing the elements of the specified
     * collection, in the order they are returned by the collection's
     * iterator.
     *
     * @param c the collection whose elements are to be placed into this list
     * @throws NullPointerException if the specified collection is null
     */
    public ArrayList(Collection<? extends E> c) {
        elementData = c.toArray();
        if ((size = elementData.length) != 0) {
            // c.toArray might (incorrectly) not return Object[] (see 6260652)
            if (elementData.getClass() != Object[].class)
                elementData = Arrays.copyOf(elementData, size, Object[].class);
        } else {
            // replace with empty array.
            this.elementData = EMPTY_ELEMENTDATA;
        }
    }

自動擴容

每當向數組中添加元素時,都要去檢查添加後元素的個數是否會超出當前數組的長度,若是超出,數組將會進行擴容,以知足添加數據的需求。數組擴容經過一個公開的方法ensureCapacity(int minCapacity)來實現。在實際添加大量元素前,我也可使用ensureCapacity來手動增長ArrayList實例的容量,以減小遞增式再分配的數量。app

數組進行擴容時,會將老數組中的元素從新拷貝一份到新的數組中,每次數組容量的增加大約是其原容量的1.5倍。這種操做的代價是很高的,所以在實際使用時,咱們應該儘可能避免數組容量的擴張。當咱們可預知要保存的元素的多少時,要在構造ArrayList實例時,就指定其容量,以免數組擴容的發生。或者根據實際需求,經過調用ensureCapacity方法來手動增長ArrayList實例的容量。框架

/**
     * Increases the capacity of this <tt>ArrayList</tt> instance, if
     * necessary, to ensure that it can hold at least the number of elements
     * specified by the minimum capacity argument.
     *
     * @param   minCapacity   the desired minimum capacity
     */
    public void ensureCapacity(int minCapacity) {
        int minExpand = (elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA)
            // any size if not default element table
            ? 0
            // larger than default for default empty table. It's already
            // supposed to be at default size.
            : DEFAULT_CAPACITY;

        if (minCapacity > minExpand) {
            ensureExplicitCapacity(minCapacity);
        }
    }

    private void ensureCapacityInternal(int minCapacity) {
        if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
            minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
        }

        ensureExplicitCapacity(minCapacity);
    }

    private void ensureExplicitCapacity(int minCapacity) {
        modCount++;

        // overflow-conscious code
        if (minCapacity - elementData.length > 0)
            grow(minCapacity);
    }

    /**
     * The maximum size of array to allocate.
     * Some VMs reserve some header words in an array.
     * Attempts to allocate larger arrays may result in
     * OutOfMemoryError: Requested array size exceeds VM limit
     */
    private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;

    /**
     * Increases the capacity to ensure that it can hold at least the
     * number of elements specified by the minimum capacity argument.
     *
     * @param minCapacity the desired minimum capacity
     */
    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);
    }

    private static int hugeCapacity(int minCapacity) {
        if (minCapacity < 0) // overflow
            throw new OutOfMemoryError();
        return (minCapacity > MAX_ARRAY_SIZE) ?
            Integer.MAX_VALUE :
            MAX_ARRAY_SIZE;
    }

add(), addAll()

跟C++ 的vector不一樣,ArrayList沒有push_back()方法,對應的方法是add(E e)ArrayList也沒有insert()方法,對應的方法是add(int index, E e)。這兩個方法都是向容器中添加新元素,這可能會致使capacity不足,所以在添加元素以前,都須要進行剩餘空間檢查,若是須要則自動擴容。擴容操做最終是經過grow()方法完成的。dom

/**
     * Appends the specified element to the end of this list.
     *
     * @param e element to be appended to this list
     * @return <tt>true</tt> (as specified by {@link Collection#add})
     */
    public boolean add(E e) {
        ensureCapacityInternal(size + 1);  // Increments modCount!!
        elementData[size++] = e;
        return true;
    }

    /**
     * Inserts the specified element at the specified position in this
     * list. Shifts the element currently at that position (if any) and
     * any subsequent elements to the right (adds one to their indices).
     *
     * @param index index at which the specified element is to be inserted
     * @param element element to be inserted
     * @throws IndexOutOfBoundsException {@inheritDoc}
     */
    public void add(int index, E element) {
        rangeCheckForAdd(index);

        ensureCapacityInternal(size + 1);  // Increments modCount!!
        System.arraycopy(elementData, index, elementData, index + 1,
                         size - index);
        elementData[index] = element;
        size++;
    }

add(int index, E e)須要先對元素進行移動,而後完成插入操做,也就意味着該方法有着線性的時間複雜度。函數

addAll()方法可以一次添加多個元素,根據位置不一樣也有兩個把本,一個是在末尾添加的addAll(Collection<? extends E> c)方法,一個是從指定位置開始插入的addAll(int index, Collection<? extends E> c)方法。跟add()方法相似,在插入以前也須要進行空間檢查,若是須要則自動擴容;若是從指定位置插入,也會存在移動元素的狀況。
addAll()的時間複雜度不只跟插入元素的多少有關,也跟插入的位置相關。

/**
     * Appends all of the elements in the specified collection to the end of
     * this list, in the order that they are returned by the
     * specified collection's Iterator.  The behavior of this operation is
     * undefined if the specified collection is modified while the operation
     * is in progress.  (This implies that the behavior of this call is
     * undefined if the specified collection is this list, and this
     * list is nonempty.)
     *
     * @param c collection containing elements to be added to this list
     * @return <tt>true</tt> if this list changed as a result of the call
     * @throws NullPointerException if the specified collection is null
     */
    public boolean addAll(Collection<? extends E> c) {
        Object[] a = c.toArray();
        int numNew = a.length;
        ensureCapacityInternal(size + numNew);  // Increments modCount
        System.arraycopy(a, 0, elementData, size, numNew);
        size += numNew;
        return numNew != 0;
    }

    /**
     * Inserts all of the elements in the specified collection into this
     * list, starting at the specified position.  Shifts the element
     * currently at that position (if any) and any subsequent elements to
     * the right (increases their indices).  The new elements will appear
     * in the list in the order that they are returned by the
     * specified collection's iterator.
     *
     * @param index index at which to insert the first element from the
     *              specified collection
     * @param c collection containing elements to be added to this list
     * @return <tt>true</tt> if this list changed as a result of the call
     * @throws IndexOutOfBoundsException {@inheritDoc}
     * @throws NullPointerException if the specified collection is null
     */
    public boolean addAll(int index, Collection<? extends E> c) {
        rangeCheckForAdd(index);

        Object[] a = c.toArray();
        int numNew = a.length;
        ensureCapacityInternal(size + numNew);  // Increments modCount

        int numMoved = size - index;
        if (numMoved > 0)
            System.arraycopy(elementData, index, elementData, index + numNew,
                             numMoved);

        System.arraycopy(a, 0, elementData, index, numNew);
        size += numNew;
        return numNew != 0;
    }

set()

既然底層是一個數組ArrayListset()方法也就變得很是簡單,直接對數組的指定位置賦值便可。

public E set(int index, E element) {
    rangeCheck(index);//下標越界檢查
    E oldValue = elementData(index);
    elementData[index] = element;//賦值到指定位置,複製的僅僅是引用
    return oldValue;
}

get()

get()方法一樣很簡單,惟一要注意的是因爲底層數組是Object[],獲得元素後須要進行類型轉換。

public E get(int index) {
    rangeCheck(index);
    return (E) elementData[index];//注意類型轉換
}

remove()

remove()方法也有兩個版本,一個是remove(int index)刪除指定位置的元素,另外一個是remove(Object o)刪除第一個知足o.equals(elementData[index])的元素。刪除操做是add()操做的逆過程,須要將刪除點以後的元素向前移動一個位置。須要注意的是爲了讓GC起做用,必須顯式的爲最後一個位置賦null值。

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; //清除該位置的引用,讓GC起做用
    return oldValue;
}

關於Java GC這裏須要特別說明一下,有了垃圾收集器並不意味着必定不會有內存泄漏。對象可否被GC的依據是是否還有引用指向它,上面代碼中若是不手動賦null值,除非對應的位置被其餘元素覆蓋,不然原來的對象就一直不會被回收。

trimToSize()

ArrayList還給咱們提供了將底層數組的容量調整爲當前列表保存的實際元素的大小的功能。它能夠經過trimToSize方法來實現。代碼以下:

/**
     * Trims the capacity of this <tt>ArrayList</tt> instance to be the
     * list's current size.  An application can use this operation to minimize
     * the storage of an <tt>ArrayList</tt> instance.
     */
    public void trimToSize() {
        modCount++;
        if (size < elementData.length) {
            elementData = (size == 0)
              ? EMPTY_ELEMENTDATA
              : Arrays.copyOf(elementData, size);
        }
    }

indexOf(), lastIndexOf()

獲取元素的第一次出現的index:

/**
     * Returns the index of the first occurrence of the specified element
     * in this list, or -1 if this list does not contain the element.
     * More formally, returns the lowest index <tt>i</tt> such that
     * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
     * or -1 if there is no such index.
     */
    public int indexOf(Object o) {
        if (o == null) {
            for (int i = 0; i < size; i++)
                if (elementData[i]==null)
                    return i;
        } else {
            for (int i = 0; i < size; i++)
                if (o.equals(elementData[i]))
                    return i;
        }
        return -1;
    }

獲取元素的最後一次出現的index:

/**
     * Returns the index of the last occurrence of the specified element
     * in this list, or -1 if this list does not contain the element.
     * More formally, returns the highest index <tt>i</tt> such that
     * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
     * or -1 if there is no such index.
     */
    public int lastIndexOf(Object o) {
        if (o == null) {
            for (int i = size-1; i >= 0; i--)
                if (elementData[i]==null)
                    return i;
        } else {
            for (int i = size-1; i >= 0; i--)
                if (o.equals(elementData[i]))
                    return i;
        }
        return -1;
    }

Fail-Fast機制

ArrayList也採用了快速失敗的機制,經過記錄modCount參數來實現。在面對併發的修改時,迭代器很快就會徹底失敗,而不是冒着在未來某個不肯定時間發生任意不肯定行爲的風險。

參考文章

  • 深刻Java集合學習系列:ArrayList的實現原理 http://zhangshixi.iteye.com/blog/674856
  • Java ArrayList源碼剖析 結合源碼對ArrayList進行講解 http://www.cnblogs.com/CarpenterLee/p/5419880.html
相關文章
相關標籤/搜索