java jdk8 parsing the source util Package: list collections source analysis

list as a set of ordered set, the user can make a very good position to master the elements in the list, you can get the data you want from the list by an integer index.

1.Collection

    collection is a collection of classes in the basic interface, a set of objects representative of this group, some sets are ordered and some are unordered, jdk does not provide any direct realization from the Collection interface, it provides more sub-interfaces implementation such as list and set, so this interface is to ensure that the set maximum versatility for the purpose of setting his method names are:

int size();

boolean isEmpty();

boolean contains(Object o);

Iterator<E> iterator();

Object[] toArray();  //返回数组

boolean add(E e);

boolean remove(Object o);

 2.ArrayList

    A size of the array is adjustable, inheritance List (Collection sub-interface) interface, a list of most of the operations, elements, and can be inserted into all types of duplicate values, including null values. In addition to the basic operations, also it provides many methods to control the internal size of the array, a non-thread synchronization. Class inheritance are as follows:

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

  transient Object[] elementData;   //存放元素的Object数组

  ...
}

add方法

 public boolean add(E e) {
        ensureCapacityInternal(size + 1);  // 完成扩容功能
        elementData[size++] = e;
        return true;
    }

Our main analysis method expansion:

  private void ensureCapacityInternal(int minCapacity) {
        if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {  //如果表为第一次创建,则初始化容器长度
            minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
        }

        ensureExplicitCapacity(minCapacity);   
    }
 private void ensureExplicitCapacity(int minCapacity) {
        modCount++;  //记录操作

        // 如果最小容量大于容器长度则需要对数组进行扩充
        if (minCapacity - elementData.length > 0)
            grow(minCapacity);
    }
  private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = elementData.length;
        int newCapacity = oldCapacity + (oldCapacity >> 1); //扩充1/2
        if (newCapacity - minCapacity < 0)
            newCapacity = minCapacity;
        if (newCapacity - MAX_ARRAY_SIZE > 0)  //如果扩充超过最大可扩充长度(最大为int的最大值)则重新取值
            newCapacity = hugeCapacity(minCapacity);
        // minCapacity is usually close to size, so this is a win:
        elementData = Arrays.copyOf(elementData, newCapacity); //创建一个新的数组
    }

remove method

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])) {  //调用equals判断是否相等
                    fastRemove(index);
                    return true;
                }
        }
        return false;
    }

    /*
     * Private remove method that skips bounds checking and does not
     * return the value removed.
     */
    private void fastRemove(int index) {
        modCount++;
        int numMoved = size - index - 1;  //需要移动的元素数量
        if (numMoved > 0)
            System.arraycopy(elementData, index+1, elementData, index, 
                             numMoved);  //调用arraycopy覆盖需要删除的值
        elementData[--size] = null; // clear to let GC do its work
    }

 get method

   public E get(int index) {
        rangeCheck(index);  //判断index是否超出范围

        return elementData(index);
    }

  E elementData(int index) {
        return (E) elementData[index]; //通过下标获取元素
    }

     The source understood add operation is smaller than the delete operation time complexity, addition is O (1), deletion is O (n), expansion and delete elements of the container is a call copyof and arrycopy methods, the underlying implementation is c ++, it will be more efficient. Volume of the container is limited, the maximum value of type int.

2.LinkList

      Belonging to the doubly linked list structure, and inherited List Deque interfaces permit insertion and repeating value any value, including a null value.

public class LinkedList<E>
    extends AbstractSequentialList<E>
    implements List<E>, Deque<E>, Cloneable, java.io.Serializable
{
    transient int size = 0;  //容量

    transient Node<E> first;  //链表第一个元素

    transient Node<E> last;  //链表最后一个元素


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


....
}

Deque: defines a method of operating a doubly-linked list, increase the versatility of subclass

AbstractSequentialList: This abstract class provides the implementation of frame to reduce the workload to achieve this interface, suitable for implementing sequentially stored list.

add method

public boolean add(E e) {
        linkLast(e);  //插入尾部连接
        return true;
    }

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

remove method

 public boolean remove(Object o) {
        if (o == null) {  
            for (Node<E> x = first; x != null; x = x.next) {
                if (x.item == null) {
                    unlink(x);
                    return true;
                }
            }
        } else {
            for (Node<E> x = first; x != null; x = x.next) {
                if (o.equals(x.item)) {
                    unlink(x);
                    return true;
                }
            }
        }
        return false;
    }

 E unlink(Node<E> x) { //x需要删除的元素
        // assert x != null;
        final E element = x.item;
        final Node<E> next = x.next;
        final Node<E> prev = x.prev;

        if (prev == null) { //如果删除的是第一个元素
            first = next;
        } else {
            prev.next = next;  
            x.prev = null;
        }

        if (next == null) { //如果删除的是最后一个元素
            last = prev;
        } else {
            next.prev = prev;
            x.next = null;
        }

        x.item = null;  //交给垃圾回收器
        size--;
        modCount++;
        return element;
    }

 get method

 public E get(int index) {
        checkElementIndex(index);
        return node(index).item;
    }

 Node<E> node(int index) {
        // assert isElementIndex(index);

        if (index < (size >> 1)) {  //判断index是否是数组长度的前段
            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;
        }
    }

linklist class, the internal structure is a doubly linked list. Each node in the linked list of objects stored before and after the reference to the element. We can be inserted to the ends of the list delete operation, in addition to that he is not limited by the capacity of the array elements can have an unlimited number of (virtual machine configuration); queries from scratch or required by the end of traversing relatively linear table for efficiency will be lower.

3.Vector

Synchronized version of ArrayList class ArrayList and implementation principle is almost the same. The only difference is that Vector is thread-safe. The main add as follows:

 public synchronized boolean add(E e) {
        modCount++;
        ensureCapacityHelper(elementCount + 1);  //确保容器容量足够
        elementData[elementCount++] = e;
        return true;
    }

The above is my understanding of the list collection.

 

 

 

 

 

Guess you like

Origin blog.csdn.net/mxy88888/article/details/91477802