公众号原文:ArrayList 源码分析
博客原文:ArrayList 源码分析
如下源码分析使用的 Java 版本为 1.8java
ArrayList 是基于数组实现的,继承 AbstractList, 实现了 List、RandomAccess、Cloneable、Serializable 接口,支持随机访问。设计模式
java.util public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
复制代码
List list = Collections.synchronizedList(new ArrayList(...));
ConcurrentModificationException
异常,可是快速失败行为不是硬保证的,只是尽最大努力当添加第一个元素时,elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA
的任何空ArrayList都将扩展为默认的capacity数组
private static final int DEFAULT_CAPACITY = 10; // 默认容量大小
private static final Object[] EMPTY_ELEMENTDATA = {}; // ArrayList空实例共享的一个空数组
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {}; // ArrayList空实例共享的一个空数组,用于默认大小的空实例。与 EMPTY_ELEMENTDATA 分开,这样就能够了解当添加第一个元素时须要建立多大的空间
transient Object[] elementData; // 真正存储ArrayList中的元素的数组
private int size; // 存储ArrayList的大小,注意不是elementData的长度
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; // 数组的最大长度
protected transient int modCount = 0; //AbstractList类的,表示 elementData在结构上被修改的次数,每次add或者remove它的值都会加1
复制代码
// 无参构造方法,默认初始容量10
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
// 提供初始容量的构造方法
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);
}
}
// 经过一个容器来初始化
public ArrayList(Collection<? extends E> c) {
elementData = c.toArray();
if ((size = elementData.length) != 0) { // c.toArray 返回的可能不是 Object[]
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
} else {
this.elementData = EMPTY_ELEMENTDATA; // replace with empty array.
}
}
复制代码
添加元素时使用 ensureCapacityInternal() 方法来保证容量足够,size + 1
为最少须要的空间大小,若是elementData的长度不够时,须要使用 grow() 方法进行扩容安全
// 添加一个元素
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
// 计算最少须要的容量
private static int calculateCapacity(Object[] elementData, int minCapacity) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
// 默认的空实例第一次添加元素时,使用默认的容量大小与minCapacity的最大值
return Math.max(DEFAULT_CAPACITY, minCapacity);
}
return minCapacity;
}
private void ensureCapacityInternal(int minCapacity) {
ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
}
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
if (minCapacity - elementData.length > 0) // 须要的容量大于elementData的长度
grow(minCapacity); // 进行扩容
}
复制代码
扩容:当新容量小于等于 MAX_ARRAY_SIZE
时,新容量的大小为 oldCapacity + (oldCapacity >> 1)
与 minCapacity
之间的较大值 ,也就是旧容量的 1.5 倍与 minCapacity
之间的较大值bash
private void grow(int minCapacity) {
int oldCapacity = elementData.length; // 本来的容量
int newCapacity = oldCapacity + (oldCapacity >> 1); // 新的容量
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
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;
}
复制代码
最后调用 Arrays.copyOf
复制原数组,将 elementData 赋值为获得的新数组。因为数组复制代价较高,因此建议在建立 ArrayList 对象时就指定大概的容量大小,减小扩容操做的次数微信
public class Arrays {
public static <T> T[] copyOf(T[] original, int newLength) {
return (T[]) copyOf(original, newLength, original.getClass());
}
public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
@SuppressWarnings("unchecked")
T[] copy = ((Object)newType == (Object)Object[].class)
? (T[]) new Object[newLength] : (T[]) Array.newInstance(newType.getComponentType(), newLength);
System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
return copy;
}
//...
}
复制代码
经过 addAll 添加一个集合中全部元素时的扩容:至少须要的容量为两个集合的长度之和,一样是经过 ensureCapacityInternal() 来保证容量是足够的,而后调用 System.arraycopy
将要添加的集合中的元素复制到原集合已有元素的后面多线程
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;
return numNew != 0;
}
复制代码
删除指定下标的元素时,若是下标没有越界,则取出下标对应的值,而后将数组中该下标后面的元素都往前挪1位,须要挪的元素数量是 size - index - 1
,时间复杂度为 O(n),因此删除元素的代价挺高并发
public E remove(int index) {
rangeCheck(index); // 检查下标是否在数组的长度范围内
modCount++;
E oldValue = elementData(index); // 下标为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;
}
private void rangeCheck(int index) {
if (index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
复制代码
删除在指定集合中的全部元素 removeAll,删除不在指定集合中的全部元素 retainAlldom
这二者都是经过 batchRemove
来批量删除源码分析
// 删除在指定集合中的全部元素
public boolean removeAll(Collection<?> c) {
Objects.requireNonNull(c); // c 不能为null
return batchRemove(c, false);
}
// 删除不在指定集合中的全部元素,也就是只保留指定集合中的元素,其它的都删除掉
public boolean retainAll(Collection<?> c) {
Objects.requireNonNull(c);
return batchRemove(c, true);
}
// 批量删除
private boolean batchRemove(Collection<?> c, boolean complement) {
final Object[] elementData = this.elementData;
int r = 0, w = 0; // r为当前下标,w为当前须要保留的元素的数量(或者说是下一个需保留元素的下标)
boolean modified = false;
try {
for (; r < size; r++)
if (c.contains(elementData[r]) == complement) // 判断元素 elementData[r] 是否须要删除
elementData[w++] = elementData[r];
} finally {
// r != size 的状况多是 c.contains() 抛出了异常,将 r 以后的元素复制到 w 以后
if (r != size) {
System.arraycopy(elementData, r, elementData, w, size - r);
w += size - r;
}
if (w != size) {
// w 以后的元素设置为 null 以让 GC 回收
for (int i = w; i < size; i++)
elementData[i] = null;
modCount += size - w;
size = w;
modified = true;
}
}
return modified;
}
复制代码
删除第一个值为指定值的元素 remove(Object o)
,参数 o 能够为 null
fastRemove(int index)
与 remove(int index)
几乎同样,只不过不返回被删除的元素
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 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
}
复制代码
ArrayList 支持三种方式:
迭代器 Iterator 和 ListIterator 的主要区别::
ArrayList 的迭代器 Iterator 和 ListIterator 在《设计模式 | 迭代器模式及典型应用》这篇文章中有过详细介绍,这里只作一个小结
foreach 循环:
foreach 循环涉及到一个 Consumer 接口,接收一个泛型的参数T,当调用 accept 方法时,Stream流中将对 accept 的参数作一系列的操做
public void forEach(Consumer<? super E> action) {
Objects.requireNonNull(action);
final int expectedModCount = modCount; // 记录当前的 modCount
@SuppressWarnings("unchecked")
final E[] elementData = (E[]) this.elementData;
final int size = this.size;
for (int i=0; modCount == expectedModCount && i < size; i++) {
action.accept(elementData[i]);
}
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
}
复制代码
ArrayList 有两个属性被 transient 关键字
修饰,transient 关键字 的做用:让某些被修饰的成员属性变量不被序列化
transient Object[] elementData;
protected transient int modCount = 0;
复制代码
为何最为重要的数组元素要用 transient 修饰呢?
跟Java的序列化机制有关,这里列出Java序列化机制的几个要点:
因此问题的答案是:ArrayList 不想用Java序列化机制的默认处理来序列化 elementData 数组,而是经过 readObject、writeObject 方法自定义序列化和反序列化策略。
问题又来了,为何不用Java序列化机制的默认处理来序列化 elementData 数组呢?
答案是由于效率问题,若是用默认处理来序列化的话,若是 elementData 的长度有100,可是实际只用了50,其实剩余的50是能够不用序列化的,这样能够提升序列化和反序列化的效率,节省空间。
如今来看 ArrayList 中自定义的序列化和反序列化策略
private static final long serialVersionUID = 8683452581122892189L;
private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException{
int expectedModCount = modCount;
s.defaultWriteObject(); // 默认的序列化策略,序列化其它的字段
s.writeInt(size); // 实际用的长度,而不是容量
for (int i=0; i<size; i++) { // 只序列化数组的前 size 个对象
s.writeObject(elementData[i]);
}
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
}
private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException {
elementData = EMPTY_ELEMENTDATA;
// Read in size, and any hidden stuff
s.defaultReadObject();
s.readInt(); // ignored
if (size > 0) {
int capacity = calculateCapacity(elementData, size);
SharedSecrets.getJavaOISAccess().checkArray(s, Object[].class, capacity);
ensureCapacityInternal(size);
Object[] a = elementData;
for (int i=0; i<size; i++) {
a[i] = s.readObject();
}
}
}
复制代码
modCount 用来记录 ArrayList 结构发生变化的次数,若是一个动做先后 modCount 的值不相等,说明 ArrayList 被其它线程修改了
若是在建立迭代器以后的任什么时候候以任何方式修改了列表(增长、删除、修改),除了经过迭代器本身的remove 或 add方法,迭代器将抛出 ConcurrentModificationException
异常
须要注意的是:这里异常的抛出条件是检测到 modCount != expectedmodCount
,若是并发场景下一个线程修改了modCount值时另外一个线程又 "及时地" 修改了expectedmodCount值,则异常不会抛出。因此不能依赖于这个异常来检测程序的正确性。
private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException{
int expectedModCount = modCount; // 记录下当前的 modCount
// 一些操做以后....
if (modCount != expectedModCount) { // 比较如今与以前的 modCount,不相等表示在中间过程当中被修改了
throw new ConcurrentModificationException();
}
}
private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException{
int expectedModCount = modCount;
// 一些操做以后....
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
}
public void forEach(Consumer<? super E> action) {
final int expectedModCount = modCount;
// 一些操做以后....
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
}
public boolean removeIf(Predicate<? super E> filter) {
final int expectedModCount = modCount;
// 一些操做以后....
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
}
public void replaceAll(UnaryOperator<E> operator) {
final int expectedModCount = modCount;
// 一些操做以后....
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
modCount++; // 修改了要加一
}
public void sort(Comparator<? super E> c) {
final int expectedModCount = modCount;
// 一些操做以后....
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
modCount++;
}
// 内部迭代器
private class Itr implements Iterator<E> {
public void forEachRemaining(Consumer<? super E> consumer) {
checkForComodification();
}
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}
复制代码
欢迎评论、转发、分享,您的支持是我最大的动力
更多内容可访问个人我的博客:laijianfeng.org
关注【小旋锋】微信公众号,及时接收博文推送