其他分享
首页 > 其他分享> > jdk 集合大家族之Collection

jdk 集合大家族之Collection

作者:互联网

jdk 集合大家族之Collection

前言:

  此处的集合指的是java集合框架中的实现了Collection接口相关的类。所以主要为List Set 和 Queue 其他章节会专门介绍Map相关。

1. List

1.1 ArrayList

1.1.1 ArrayList分析 (jdk 8)

从ArrayList 的初始化开始说起
    private static final long serialVersionUID = 8683452581122892189L;

    /**
     * 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;
    
    // 数组最大的长度 其中通过最终的hugeCapacity()可以突破这个限制达到Integer.MAX_VALUE
    // 数组需要-8: 有些虚拟机在数组中保留了一些头信息。避免内存溢出
     /**
     * 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;

需要注意的是ArrayList存储的最大量其实跟你设置的内存也是有关系的。

ArrayList 的删除操作
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;
    }

    /*
     * 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)
            // 将index后面的舒服复制到index处
            System.arraycopy(elementData, index+1, elementData, index,
                             numMoved);
                             //置空
        elementData[--size] = null; // clear to let GC do its work
    }
ArrayList 的扩容
   // 添加函数
   public boolean add(E e) {
        ensureCapacityInternal(size + 1);  // Increments modCount!!
        elementData[size++] = e;
        return true;
    }
     private void ensureCapacityInternal(int minCapacity) {
        ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
    }
    private static int calculateCapacity(Object[] elementData, int minCapacity) {
        if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
            return Math.max(DEFAULT_CAPACITY, minCapacity);//空数组扩容默认值和需要的长度 取最大值
        }
        return minCapacity;
    }
    private void ensureExplicitCapacity(int minCapacity) {
        // modCount: 继承自AbstractList 用于防止一个迭代器(Iterator)对集合进行修改而另一个迭代器对其进行遍历时,产生混乱的现象(并发场景下)。与modCount 搭配的是 expectedModCount
        modCount++;

        // overflow-conscious code
        // 需要的长度比数组长度大,此时确实需要扩容
        if (minCapacity - elementData.length > 0)
            grow(minCapacity);
    }
     private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = elementData.length;
        // 目标扩容为原数组长度的1.5倍
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        // 需要的长度比计算值大的话 取最大值
        if (newCapacity - minCapacity < 0)
            newCapacity = minCapacity;
        // 如果计算后的值比ArrayList 的MAX_ARRAY_SIZE属性还要大的话,进行终极扩容
        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();
            // 最大值为Integer.MAX_VALUE 此时多余的会被舍弃掉
        return (minCapacity > MAX_ARRAY_SIZE) ?
            Integer.MAX_VALUE :
            MAX_ARRAY_SIZE;
    }

1.2 LinkedList

1.3 Vector

2. Set

2.1 HashSet

散列集

2.2 TreeSet

3. Queue

3.2 LinkedList

3.3 PriorityQueue

优先级队列

比较

参考资料

标签:index,大家族,jdk,int,ArrayList,elementData,Collection,minCapacity,size
来源: https://www.cnblogs.com/wei57960/p/12194508.html