List实现类源码学习

List接口继承了Collection, 其下有三个实现类:ArrayList、LinkedList、Vector。

区别:

1、ArrayList:底层是通过数组来实现的,是一个顺序容器,允许我们放入null元素,线程不安全,查询效率快但增删慢,每当容量不足则进行扩容操作,默认初始长度为10,JDk1.7每次扩容是在原容量长度*1.5+1,JDK8是原长度*1.5。

2、LinkedList:底层是双向链表的结构,线程不安全, 高效的插入和移除操作但查询慢

3、Vector:是一个可实现自动增长的数组结构,线程安全,查询快但增删慢, 每当容量不足则在当前容量长度*2

源码分析:

一、ArrayList:

	   /**
     * 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;

    DEFAULT_CAPACITY:为默认初始化容量10

    EMPTY_ELEMENTDATA :定义容量为0的集合时使用这个空数组替换,可以看下面构造方法源码

    DEFAULTCAPACITY_EMPTY_ELEMENTDATA :未定义数组容量,既创建集合未给容量时用这个空数组替换

    elementDate:底层使用的数组,对这个数组进行操作

    Size: 实际ArrayList集合大小 也就是实际元素的个数

构造函数的源码:(可看到 EMPTY_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;
    }

    可看出构造容量为0的集合时,用 EMPTY_ELEMENTDATA 进行替换。

    无参构造集合时,用 DEFAULTCAPACITY_EMPTY_ELEMENTDATA  替换,且在添加元素的时候,容量自动扩充为  10(DEFAULT_CAPACITY的值)。

添加源码:

    /**
     * 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(E e) 在数组后面添加一个新元素e, 函数ensureCapacityInternal主要是对数组的容量进行操作

    add(int index, E element) 在数组指定位置插入一个新元素e,函数rangeCheckForAdd主要判断index的值是否合法,ensureCapacityInternal同上, System.arraycopy主要是为了将elementData数组的index下标之后的所有元素后移一位。

移除源码:

    /**
     * Removes the element at the specified position in this list.
     * Shifts any subsequent elements to the left (subtracts one from their
     * indices).
     *
     * @param index the index of the element to be removed
     * @return the element that was removed from the list
     * @throws IndexOutOfBoundsException {@inheritDoc}
     */
    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(int index) 移除指定位置的元素并返回该元素,函数rangeCheck判断index是否合法,modCount变量为迭代函数等使用的,System.arraycopy主要是index位置的元素移除数组,elementData[--size] = null主要将最后一个元素设为null同时数组长度减1

    remove(Object o)方法自己看源码,整体逻辑差不多

get、set源码比较简单,这里不解释

inertor(迭代器)源码:

    public Iterator<E> iterator() {
        return new Itr();  //Itr是其内部类
    }
    /**
     * An optimized version of AbstractList.Itr
     */
    private class Itr implements Iterator<E> {
        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<? super E> 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();
        }
    }

cursor表示下一个元素,初始值为0;lastRet表示最后一个元素,初始值为-1;modCount代表该List对象被修改的次数,每对List对象修改一次,modCount都会加1

函数 next() 分析:

1、通过 checkForComodification() 函数判断 expectedModCount 和 modCount 是否相等
2、然后去看cursor是不是超过集合的大小和数组的长度
3、cursor+1,既下标加一继续表下一个元素
4、当前元素下标赋给 lastRet 同时返回该下标下的元素
5、该方法执行完后 cursor 和 lastRet 都加一

注意:集合在执行for循环(实际也是使用了迭代器)的时候不建议进行remove操作,因为remove改变了集合大小, 有可能出现数组下标越界异常
因为在执行List集合在执行remove方法的时候会对modCount进行加一操作(具体可以看移除的源码),而expectedModCount是在执行迭代器,即创建 new Itr() 对象的时候就赋了modCount的值。这就导致expectedModCount和modCount不一致,既在执行迭代器next() 方法的时候在由checkForComodification方法抛出了ConcurrentModificationException异常。

for循环中执行add方法同样会出现这个异常,都是因为modCount和expectedModCount不相等导致的。

函数 remove() 分析:

1、判断lastRet是否小于0,既调该方法前要执行一次next()方法
2、通过 checkForComodification() 函数判断 expectedModCount 和 modCount 是否相等
3、删除下标 lastRet 的元素,然后lastRet 赋值给lastRet来表示下一个元素,同时 lastRet -1
4、最后把modCount重新赋值给expectedModCount

二、LinkedList:

每个元素节点含有三部分:prev、element、next

1、prev是存储的上一个节点的引用。

2、element是存储的具体的内容。

3、next是存储的下一个节点的引用。

4、另外,因为LinkedList不是一个循环的双向链表,所以他第一个节点的prev以及最后一个节点的next都是null

    transient int size = 0;

    /**
     * Pointer to first node.
     * Invariant: (first == null && last == null) ||
     *            (first.prev == null && first.item != null)
     */
    transient Node<E> first;

    /**
     * Pointer to last node.
     * Invariant: (first == null && last == null) ||
     *            (last.next == null && last.item != null)
     */
    transient Node<E> last;

    size:集合元素个数

    first:第一个节点的指针

    last:最后一个节点的指针

构造函数的源码

   /**
     * Constructs an empty list.
     */
    public LinkedList() {
    }

    /**
     * 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 LinkedList(Collection<? extends E> c) {
        this();
        addAll(c);
    }

    LinkedList(Collection<? extends E> c) 函数会将集合c所有元素插入该集合中

节点源码:

    private static class Node<E> {
        E item;
        Node<E> next;
        Node<E> prev;

        Node(Node<E> prev, E element, Node<E> next) {
            this.item = element;
            this.next = next;
            this.prev = prev;
        }
    }

    E:表示该元素值

    next:表示该元素节点的下一个节点引用,链表最后一个元素该值为null

    prev:表示该元素节点的下一个节点引用,链表第一个元素该值为null

添加源码:

    public boolean add(E e) {
        linkLast(e);
        return true;
    }

    /**
     * Inserts the specified element at the beginning of this list.
     *
     * @param e the element to add
     */
    public void addFirst(E e) {
        linkFirst(e);
    }

    /**
     * Appends the specified element to the end of this list.
     *
     * <p>This method is equivalent to {@link #add}.
     *
     * @param e the element to add
     */
    public void addLast(E e) {
        linkLast(e);
    }

    /**
     * Links e as first element.
     */
    private void linkFirst(E e) {
        final Node<E> f = first;
        final Node<E> newNode = new Node<>(null, e, f);
        first = newNode;
        if (f == null)
            last = newNode;
        else
            f.prev = newNode;
        size++;
        modCount++;
    }

    /**
     * Links e as last element.
     */
    void linkLast(E e) {
        final Node<E> l = last;
        final Node<E> newNode = new Node<>(l, e, null);
        last = newNode;
        if (l == null)
            first = newNode;
        else
            l.next = newNode;
        size++;
        modCount++;
    }

    函数 add(E e)、addLast(E e) 都是在集合后面添加新元素,函数 addFirst(E e) 表在集合最前添加新元素。

    函数 linkLast(E e) 解析 : 表示在集合最后添加一个元素
      l:表示未添加新元素前,集合的最后一个节点对象
      newNode:表示创建一个新的节点,该节点前一个节点引用为l,后一个节点引用为null,值为e
      之后将集合最后一个元素last设置成  newNode,判断该集合未添加新元素前是否为空,为空此则为第一个元素,不是空则将原先最后一个元素 l 的下一个引用设为 newNode,同时集合长度加一。

    public boolean addAll(Collection<? extends E> c) {
        return addAll(size, c);
    }

    public boolean addAll(int index, Collection<? extends E> c) {
        checkPositionIndex(index);

        Object[] a = c.toArray();
        int numNew = a.length;
        if (numNew == 0)
            return false;

        Node<E> pred, succ;
        if (index == size) {
            succ = null;
            pred = last;
        } else {
            succ = node(index);
            pred = succ.prev;
        }

        for (Object o : a) {
            @SuppressWarnings("unchecked") 
            E e = (E) o;
            Node<E> newNode = new Node<>(pred, e, null);
            if (pred == null)
                first = newNode;
            else
                pred.next = newNode;
            pred = newNode;
        }

        if (succ == null) {
            last = pred;
        } else {
            pred.next = succ;
            succ.prev = pred;
        }

        size += numNew;
        modCount++;
        return true;
    }

    函数addAll(Collection<? extends E> c) 是将一个集合添加到该链表尾部,

    1、通过 checkPositionIndex 判断index是否合法
    2、将要添加的集合转数组同时验证长度是否正确
    3、判断是否加到链表尾部,若是,则succ设置为null,pred设置为原先集合的最后一个节点;不是,则succ设置为index下标的节点,pred设置为index的前一个节点引用。
    4、循环遍历新添加的集合,创建成新节点并添加到原先的集合中。
    5、循环结束后,判断新添加的集合是否加在集合尾部,若是则集合最后一个元素为新集合的最后一个元素,若不是则修改集合插入位置最后元素、原先index元素的前后引用。
    6、修改集合长度

移除操作:主要也是对节点以及其前后的引用进行操作,这里不做分析。
三、Vector

Vector 的源码中添加、移除等很多方法都添加了 synchronized 关键字进行修饰,这才使其线程安全,其他逻辑和上面的集合大同小异。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值