【JDK源码】ArrayList解析

一、前言

       第一次写技术类博客,对相关的知识做个总结和归纳,养成自己的一个习惯吧,不当或错误之处还望多多指教。

二、ArrayList类

       ArrayList继承了AbstractList类实现了List接口,拥有集合的基本方法;Serializable接口,因此它支持序列化,能够通过序列化传输;RandomAccess接口,支持快速随机访问,实际上就是通过下标序号进行快速访问;实现了Cloneable接口,能被克隆。

三、ArrayList属性

       ArrayList的关键属性是elementData(数组)和size(大小),数组容量大于等于数组大小。

    // 默认初始容量
    private static final int DEFAULT_CAPACITY = 10;
    // 空数组对象
    private static final Object[] EMPTY_ELEMENTDATA = {};
    // 空数组对象(新增第一个元素扩充为默认容量)
    private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
    // 数组对象(不可被序列化)
    transient Object[] elementData; // non-private to simplify nested class access
    // 数组大小(小于或等于数组容量)
    private int size;  

四、ArrayList构造器

       ArrayList总共四个构造函数,建议在集合初始化时声明大小能够有效减少数组扩容消耗。

    // 构造指定容量大小的空数组
    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);
        }
    }
    // 构造空数组(未指定大小初次新增元素扩容为默认容量大小)
    public ArrayList() {
        this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
    }
    // 初始化包含特殊元素的数组对象(初始化的容量大小等于数组大小)
    public ArrayList(Collection 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;
        }
    }

五、ArrayList核心函数

       首先提下类中用到的其余几个函数,便于后续的理解。
       System.arraycopy(Object src, int srcPos,Object dest, int destPos,int length)拷贝函数,将src源对象中srcPos至srcPos+length数据拷贝到destPos目标对象destPos位置。
       Arrays.copyOf(U[] original, int newLength, Class newType)拷贝函数,将数组original中从0个元素开始拷贝newLength到临时数组,数组元素类型一致,并返回临时数组,其实底层还是调用的System.arraycopy函数。

public static  T[] copyOf(U[] original, int newLength, Class newType) {
        @SuppressWarnings("unchecked")
        T[] copy = ((Object)newType == (Object)Object[].class)
            ? (T[]) new Object[newLength]
            : (T[]) Array.newInstance(newType.getComponentType(), newLength);
        System.arraycopy(original, 0, copy, 0,
                         Math.min(original.length, newLength));
        return copy;
    }

       trimToSize()方法是将数组容量置为数组大小以便节省内存,modCount是抽象类AbstractList的属性,后面会详细说明。

// 调整数组将容量大小置为数组大小
    public void trimToSize() {
        modCount++;
        if (size < elementData.length) {
            elementData = (size == 0)
                    ? EMPTY_ELEMENTDATA
                    : Arrays.copyOf(elementData, size);
        }
    }

       grow(int minCapacity)是扩容函数,默认扩容大小=oldCapacity + (oldCapacity >> 1),约原数组容量的1.5倍,然后再与指定扩容值和最大值进行比较。

// 默认扩容大小约为原数组容量1.5倍(原数组容量可能为奇数)
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);
    }

       indexOf(Object o)函数返回第一个出现目标元素的位置,如果没有找到则返回-1,循环数组遍历查找;lastIndexOf(Object o)函数也是类似,这里就不上源码了。

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;
    }

       toArray(T[] a)函数将集合转换为数组,如果集合大小小于数组长度,则直接拷贝集合元素并补齐null值,否则返回一个长度等于集合大小的新数组。

public  T[] toArray(T[] a) {
        if (a.length < size)
            // Make a new array of a's runtime type, but my contents:
            return (T[]) Arrays.copyOf(elementData, size, a.getClass());
        System.arraycopy(elementData, 0, a, 0, size);
        if (a.length > size)
            a[size] = null;
        return a;
    }

       get(int index)函数直接通过索引直接返回元素,需要检查索引是否合理。

public E get(int index) {
        rangeCheck(index);

        return elementData(index);
    }

       set(int index, E element)函数设置索引位置对应的值,并返回修改前的元素。

public E set(int index, E element) {
        rangeCheck(index);

        E oldValue = elementData(index);
        elementData[index] = element;
        return oldValue;
    }

       add(E e)函数表示在最后新增元素,首先会检查是否要扩容,然后再进行元素添加,添加成功返回true。

public boolean add(E e) {
        ensureCapacityInternal(size + 1);  // Increments modCount!!
        elementData[size++] = e;
        return true;
    }

       add(int index, E element)函数在指定位置插入指定元素,首先检查索引合理性和数组容量是否够,然后将index到最后的元素拷贝到index+1位置,最后index位置插入指定元素。

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++;
    }

       remove(int index)函数移除指定位置元素,其原理是将index+1到最后的元素拷贝到index位置,然后将最后的元素置为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; // clear to let GC do its work

        return oldValue;
    }

       remove(Object o)函数移除指定对象,循环遍历数组找到对应元素索引,然后调用fastRemove函数删除对应索引处的元素。fastRemove和remove函数基本一致,没有索引校验和返回值。

public boolean remove(Object o) {
        if (o == null) {
            for (int index = 0; index < size; index++)
                if (elementData[index] == null) {
                    fastRemove(index);
                    return true;
                }
        } else {
            for (int index = 0; index < size; index++)
                if (o.equals(elementData[index])) {
                    fastRemove(index);
                    return true;
                }
        }
        return false;
    }

       clear()函数清除所有元素,循环遍历置为null(清除工作交给GC)。

public void clear() {
        modCount++;

        // clear to let GC do its work
        for (int i = 0; i < size; i++)
            elementData[i] = null;

        size = 0;
    }

       addAll(Collection c)添加集合元素,首先保证数组容量足够然后调用系统函数直接拷贝。

public boolean addAll(Collection 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;
    }

       addAll(int index, Collection c)函数在指定位置添加集合元素,会存在覆盖的情况。

public boolean addAll(int index, Collection 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;
    }

       removeRange(int fromIndex, int toIndex)函数移除索引fromIndex到toIndex的元素,原理是将toIndex到最后的元素拷贝到fromIndex,然后将后面的元素至为null。

protected void removeRange(int fromIndex, int toIndex) {
        modCount++;
        int numMoved = size - toIndex;
        System.arraycopy(elementData, toIndex, elementData, fromIndex,
                         numMoved);

        // clear to let GC do its work
        int newSize = size - (toIndex-fromIndex);
        for (int i = newSize; i < size; i++) {
            elementData[i] = null;
        }
        size = newSize;
    }

       batchRemove(Collection c, boolean complement)函数移除或者保留集合c中的元素,主要是removeAll(Collection c)(删除集合元素)和retainAll(Collection c)(求两集合的公共元素)进行调用。

    private boolean batchRemove(Collection c, boolean complement) {
        final Object[] elementData = this.elementData;
        int r = 0, w = 0;
        boolean modified = false;
        try {
            // 以retainAll为例,如果集合c中存在某元素,则直接将某元素覆盖到原数组elementData当中(从起始位置开始)
            for (; r < size; r++)
                if (c.contains(elementData[r]) == complement)
                    elementData[w++] = elementData[r];
        } finally {
            // 如果出现异常,则将r到size数据拷贝到w位置覆盖并调整w大小
            if (r != size) {
                System.arraycopy(elementData, r,
                        elementData, w,
                        size - r);
                w += size - r;
            }
            // 不管前面是否出现异常,将w到size的数据置为null(也就是删除)
            if (w != size) {
                // clear to let GC do its work
                for (int i = w; i < size; i++)
                    elementData[i] = null;
                modCount += size - w;
                size = w;
                modified = true;
            }
        }
        return modified;
    }

       内部类ListItr继承内部类Itr,是ArrayList集合中Iterator()方法的实现,modCount是用来记录集合ArrayList修改次数,在迭代器遍历的过程中需要保证别的线程没有对数组进行操作(防止出现不可思议的错误),否则抛出ConcurrentModificationException异常,这就是所谓的Fail-Fast 机制 ,由此可知ArrayList是非线程安全的。

private class Itr implements Iterator {
        int cursor;       // index of next element to return
        int lastRet = -1; // index of last element returned; -1 if no such
        int expectedModCount = modCount;

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

        @SuppressWarnings("unchecked")
        public E next() {
            checkForComodification();
            int i = cursor;
            if (i >= size)
                throw new NoSuchElementException();
            Object[] elementData = ArrayList.this.elementData;
            if (i >= elementData.length)
                throw new ConcurrentModificationException();
            cursor = i + 1;
            return (E) elementData[lastRet = i];
        }

        public void remove() {
            if (lastRet < 0)
                throw new IllegalStateException();
            checkForComodification();

            try {
                ArrayList.this.remove(lastRet);
                cursor = lastRet;
                lastRet = -1;
                expectedModCount = modCount;
            } catch (IndexOutOfBoundsException ex) {
                throw new ConcurrentModificationException();
            }
        }

        @Override
        @SuppressWarnings("unchecked")
        public void forEachRemaining(Consumer consumer) {
            Objects.requireNonNull(consumer);
            final int size = ArrayList.this.size;
            int i = cursor;
            if (i >= size) {
                return;
            }
            final Object[] elementData = ArrayList.this.elementData;
            if (i >= elementData.length) {
                throw new ConcurrentModificationException();
            }
            while (i != size && modCount == expectedModCount) {
                consumer.accept((E) elementData[i++]);
            }
            // update once at end of iteration to reduce heap write traffic
            cursor = i;
            lastRet = i - 1;
            checkForComodification();
        }
  
        // 在遍历过程中检测数组是否有修改
        final void checkForComodification() {
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
        }
    }

       removeIf(Predicate filter)函数删除指定条件的元素,Predicate是Java8的语法,用来检测元素是否满足条件。

@Override
    public boolean removeIf(Predicate filter) {
        Objects.requireNonNull(filter);
        // 使用removeCount和removeSet来记录需要移除元素的数量和位置
        // BitSet可以看做一个010100110二进制数,节省内存
        int removeCount = 0;
        final BitSet removeSet = new BitSet(size);
        final int expectedModCount = modCount;
        final int size = this.size;
        for (int i=0; modCount == expectedModCount && i < size; i++) {
            @SuppressWarnings("unchecked")
            final E element = (E) elementData[i];
            if (filter.test(element)) {
                // 此处将需要移除的元素标记为1
                removeSet.set(i);
                removeCount++;
            }
        }
        if (modCount != expectedModCount) {
            throw new ConcurrentModificationException();
        }

        // shift surviving elements left over the spaces left by removed elements
        final boolean anyToRemove = removeCount > 0;
        if (anyToRemove) {
            final int newSize = size - removeCount;
            for (int i=0, j=0; (i < size) && (j < newSize); i++, j++) {
                // 从第i个位置开始获取第一个不为0的索引,也就是数组需要保留的元素
                i = removeSet.nextClearBit(i);
                // 直接将第i个元素覆盖掉第j个,i一定大于等于j
                elementData[j] = elementData[i];
            }
            // 移除newSize后续的元素
            for (int k=newSize; k < size; k++) {
                elementData[k] = null;  // Let gc do its work
            }
            this.size = newSize;
            if (modCount != expectedModCount) {
                throw new ConcurrentModificationException();
            }
            modCount++;
        }

        return anyToRemove;
    }

       replaceAll(UnaryOperator operator)函数替换指定条件的元素,UnaryOperator为Funtion接口,为JDK8的语法,具体用法自行查阅。

public void replaceAll(UnaryOperator operator) {
        Objects.requireNonNull(operator);
        final int expectedModCount = modCount;
        final int size = this.size;
        for (int i=0; modCount == expectedModCount && i < size; i++) {
            elementData[i] = operator.apply((E) elementData[i]);
        }
        if (modCount != expectedModCount) {
            throw new ConcurrentModificationException();
        }
        modCount++;
    }

       sort(Comparator c)函数用来对元素进行排序,比较器自定义。

public void sort(Comparator c) {
        final int expectedModCount = modCount;
        Arrays.sort((E[]) elementData, 0, size, c);
        if (modCount != expectedModCount) {
            throw new ConcurrentModificationException();
        }
        modCount++;
    }

六、总结

  •        如果在初始化ArrayList时知道大小,最好声明大小以减少扩容的消耗。
  •        ArrayList是线程不安全的,多线程操作是需要特别注意。
  •        属性elementData数组声明为transient,无法进行序列化。

你可能感兴趣的:(【JDK源码】ArrayList解析)