编程语言
首页 > 编程语言> > Collection源码

Collection源码

作者:互联网

ArrayList源码  
 
  /**
     *添加元素,
     */

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

    /**
     *ArrayList初始为空数组{},minCapacity第一次为1==》默认长度10
     */
    private void ensureCapacityInternal(int minCapacity) {
        if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
            minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
        }

        ensureExplicitCapacity(minCapacity);
    }

    /**
     * 当minCapacity小于数组长度时==》扩容grow
     */
    private void ensureExplicitCapacity(int minCapacity) {
        modCount++;

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

    /**
     * 新的数组扩容为1.5倍,
     */
    private void grow(int minCapacity) {
     
        int oldCapacity = elementData.length;
        int newCapacity = oldCapacity + (oldCapacity >> 1); //1.5倍原数组长度扩容
        
        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); //
    }
 
Vector源码:

   /**
     * Vector的无参构造会默认指定10大小,
     */
    public Vector() {
        this(10);
    }
   /**
     * 第一次容量增长系数:capacityIncrement为0
     */
    public Vector(int initialCapacity) {
        this(initialCapacity, 0);
    }
    public Vector(int initialCapacity, int capacityIncrement) {
        super();
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        this.elementData = new Object[initialCapacity];
        this.capacityIncrement = capacityIncrement;
    }
======
   /**
     * 添加 synchronized 修饰==》线程安全
     */
    public synchronized boolean add(E e) {
        modCount++;
        ensureCapacityHelper(elementCount + 1);
        elementData[elementCount++] = e;
        return true;
    }

   /**
     * 当最小容量小于数组长度时==》扩容
     */
    private void ensureCapacityHelper(int minCapacity) {
        // overflow-conscious code
        if (minCapacity - elementData.length > 0)
            grow(minCapacity);
    }
   /**
     * capacityIncrement 第一次为0,后面为10,==》每次扩容2倍
     */
private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = elementData.length;
        int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
                                         capacityIncrement : oldCapacity);
        if (newCapacity - minCapacity < 0)
            newCapacity = minCapacity;
        if (newCapacity - MAX_ARRAY_SIZE > 0)
            newCapacity = hugeCapacity(minCapacity);
        elementData = Arrays.copyOf(elementData, newCapacity);
    }

LinkedList源码:

    /**
     * LinkedList新增,双向链表
     */
    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;
        }
    }

    public boolean add(E e) {
        linkLast(e);
        return true;
    }
    /**
     * Linked首次first 、last 为同一节点,后续默认插入到最后节点
     */
    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++;
    }

HashSet源码:

 

 

 

 
 

 

标签:Node,capacityIncrement,int,elementData,newCapacity,Collection,minCapacity,源码
来源: https://www.cnblogs.com/Jiedeman/p/16365116.html