集合框架总结

  2019作为新的一年开始,我也着手面试的准备。这篇的博客的主角集合--面试中都会出现的,所以今天特作此总结,也算是复习的成果的一个展示。在查看了许多的博客和源码后我决定将其分成3部分来总结。

三个部分分别是:集合的分类、各个集合的底层实现、集合方法的源码实现

                                    集合的分类

  1.集合-- collection接口

 

  

先上个图

collection作为Set, List, Queue三个接口的父接口,它定义了操作集合的公用方法,包括add(),remove()等方法。稍后会对这些常用的方法进行源码解读。集合作为存储数据的容器,它的作用有点像数据库的作用,而接口则提供了操作这些数据的方法。

下面来看下每个集合的底层数据结构是怎么实现的不废话先上图

在知道了集合的分类和底层实现后,在来看下最经常使用的几个集合的源码

1、ArrarList作为最经常使用的集合。

  1. 类声明如下:
public class ArrayList<E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable

实现的后三个接口分别表示,该集合可以随机访问(通过下标访问),克隆,序列化和反序列化(把对象转换为字节序列的过程称为对象的序列化。把字节序列恢复为对象的过程称 为

对象的反序列化。)

实现的第一个接口List则表示ArrayList可以使用List接口的所有方法。

  2. 类的成员属性

public class ArrayList<E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
    // 版本号
    private static final long serialVersionUID = 8683452581122892189L;
    // 缺省容量
    private static final int DEFAULT_CAPACITY = 10;
    // 空对象数组
    private static final Object[] EMPTY_ELEMENTDATA = {};
    // 缺省空对象数组
    private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
    // 元素数组
    transient Object[] elementData;
    // 实际元素大小,默认为0
    private int size;
    // 最大数组容量
    private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
}

说明:类的属性中核心的属性为elementData,类型为Object[],用于存放实际元素,并且被标记为transient,也就意味着在序列化的时候,此字段是不会被序列化的。

  3.类的构造函数ArrayList(int)--初始化指定大小的集合

public ArrayList(int initialCapacity) {
        if (initialCapacity > 0) { // 初始容量大于0
            this.elementData = new Object[initialCapacity]; // 初始化元素数组
        } else if (initialCapacity == 0) { // 初始容量为0
            this.elementData = EMPTY_ELEMENTDATA; // 为空对象数组
        } else { // 初始容量小于0,抛出异常
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        }
    }

说明:指定elementData数组的大小,不允许初始化大小小于0,否则抛出异常。

ArrayList()无参构造函数,当初始化时会给一个默认10的容量大小
 /**
     * Constructs an empty list with an initial capacity of ten.
     */
    public ArrayList() {
        this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
    }
 ArrayList(Collection<? extends E> c) 参数为泛型的集合参数,即传入的参数必须是集合的子类
 /**
     * 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 ArrayList(Collection<? extends E> c) {
      // 将传入的集合转为数组 elementData
= c.toArray(); if ((size = elementData.length) != 0) { // c.toArray might (incorrectly) not return Object[] (see 6260652)
      // 可能不返回object[]数组
if (elementData.getClass() != Object[].class)
      // 拷贝数据到object[]数组 elementData
= Arrays.copyOf(elementData, size, Object[].class); } else { // replace with empty array. this.elementData = EMPTY_ELEMENTDATA; } }

   4.add方法

public boolean add(E e) {
    //对数组的容量进行调整
    ensureCapacityInternal(size + 1);  // Increments modCount!!
    elementData[size++] = e;
    return true;
}

//在指定位置添加一个元素
public void add(int index, E element) {
// 数组下标校验 rangeCheckForAdd(index);
//对数组的容量进行调整 ensureCapacityInternal(size + 1); // Increments modCount!! // 把index后的数据往后移一位,在index位置插入新元素 System.arraycopy(elementData, index, elementData, index + 1, size - index); elementData[index] = element; size++; } //添加一个集合 public boolean addAll(Collection<? extends E> c) { //把该集合转为对象数组 Object[] a = c.toArray(); int numNew = a.length; //增加容量 ensureCapacityInternal(size + numNew); // Increments modCount //挨个向后迁移 System.arraycopy(a, 0, elementData, size, numNew); size += numNew; //新数组有元素,就返回 true return numNew != 0; } //在指定位置,添加一个集合 public boolean addAll(int index, Collection<? extends E> 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; }

  5.数组容量调整

public void ensureCapacity(int minCapacity) {
    int minExpand = (elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA)
        // 不是默认的数组,说明已经添加了元素
        ? 0
        // 默认的容量
        : DEFAULT_CAPACITY;

    if (minCapacity > minExpand) {
        //当前元素个数比默认容量大
        ensureExplicitCapacity(minCapacity);
    }
}

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

  6.扩容

private void grow(int minCapacity) {
    int oldCapacity = elementData.length;
    // 1.5 倍 原来容量
    int newCapacity = oldCapacity + (oldCapacity >> 1);

    //如果当前容量还没达到 1.5 倍旧容量,就使用当前容量,省的站那么多地方
    if (newCapacity - minCapacity < 0)
        newCapacity = minCapacity;

    //新的容量居然超出了 MAX_ARRAY_SIZE
    if (newCapacity - MAX_ARRAY_SIZE > 0)
        //最大容量可以是 Integer.MAX_VALUE
        newCapacity = hugeCapacity(minCapacity);

    // minCapacity 一般跟元素个数 size 很接近,所以新建的数组容量为 newCapacity 更宽松些
    elementData = Arrays.copyOf(elementData, newCapacity);
}

private static int hugeCapacity(int minCapacity) {
    if (minCapacity < 0) // overflow
        throw new OutOfMemoryError();
    return (minCapacity > MAX_ARRAY_SIZE) ?
        Integer.MAX_VALUE :
        MAX_ARRAY_SIZE;
}

                       

 7.查询和修改

E elementData(int index) {
    return (E) elementData[index];
}

//获取
public E get(int index) {
    rangeCheck(index);
    //直接根据数组角标返回元素,快的一比
    return elementData(index);
}

//修改
public E set(int index, E element) {
    rangeCheck(index);
    E oldValue = elementData(index);

    //直接对数组操作
    elementData[index] = element;
    //返回原来的值
    return oldValue;
}

8、删除

//根据位置删除
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;
}

//删除某个元素
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;
}

//内部方法,“快速删除”,就是把重复的代码移到一个方法里
//没看出来比其他 remove 哪儿快了 - -
private void fastRemove(int index) {
    modCount++;
    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
}

//保留公共的
public boolean retainAll(Collection<?> c) {
    Objects.requireNonNull(c);
    return batchRemove(c, true);
}

//删除或者保留指定集合中的元素
private boolean batchRemove(Collection<?> c, boolean complement) {
    final Object[] elementData = this.elementData;
    //使用两个变量,一个负责向后扫描,一个从 0 开始,等待覆盖操作
    int r = 0, w = 0;
    boolean modified = false;
    try {
        //遍历 ArrayList 集合
        for (; r < size; r++)
            //如果指定集合中是否有这个元素,根据 complement 判断是否往前覆盖删除
            if (c.contains(elementData[r]) == complement)
                elementData[w++] = elementData[r];
    } finally {
        //发生了异常,直接把 r 后面的复制到 w 后面
        if (r != size) {
            System.arraycopy(elementData, r,
                             elementData, w,
                             size - r);
            w += size - r;
        }
        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;
}

//清楚全部
public void clear() {
    modCount++;
    //并没有直接使数组指向 null,而是逐个把元素置为空
    //下次使用时就不用重新 new 了
    for (int i = 0; i < size; i++)
        elementData[i] = null;

    size = 0;
}

9、indexof和lastindexof

public boolean contains(Object o) {
    return indexOf(o) >= 0;
}

//遍历,第一次找到就返回
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;
}

//倒着遍历
public int lastIndexOf(Object o) {
    if (o == null) {
        for (int i = size-1; i >= 0; i--)
            if (elementData[i]==null)
                return i;
    } else {
        for (int i = size-1; i >= 0; i--)
            if (o.equals(elementData[i]))
                return i;
    }
    return -1;
}

由于 ArrayList 不是同步的,所以在并发访问时,如果在迭代的同时有其他线程修改了 ArrayList, fail-fast 的迭代器 Iterator/ListIterator 会报 ConcurrentModificationException 错。
因此我们在并发环境下需要外部给 ArrayList 加个同步锁,或者直接在初始化时用 Collections.synchronizedList 方法进行包装:

List list = Collections.synchronizedList(new ArrayList(...));

 

由于第一次写总结博客,的确是中费时费力的工作,但也收获颇多,如有不足还请轻喷。

参考博客:

https://blog.csdn.net/u011240877/article/details/52853989

https://blog.csdn.net/u011518120/article/details/52026076

https://www.cnblogs.com/leesf456/p/5308358.html

 

posted @ 2019-02-20 14:39  主宰-  阅读(317)  评论(0编辑  收藏  举报