public interface Collection<E> extends Iterable<E>
源码分析:
ArrayList中维护了一个Object类型的数组elementData。transient Object[] elementData(transient 表示瞬间,短暂的,该属性不会被序列化)
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
public boolean add(E e) {
//确认大小够不够我添加一个数据,先确定是否要扩容
ensureCapacityInternal(size + 1); // Increments modCount!!
//然后再执行赋值操作
elementData[size++] = e;
return true;
}
private void ensureCapacityInternal(int minCapacity) {
ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
}
//该方法确定minCapacity
private static int calculateCapacity(Object[] elementData, int minCapacity) {
//如果elementData是{}
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
// 1 和 默认10 找一个最大值
return Math.max(DEFAULT_CAPACITY, minCapacity);
}
//第二次添加时,elementData不是{},直接返回
return minCapacity;
}
private void ensureExplicitCapacity(int minCapacity) {
//记录当前集合修改次数,防止有多个线程来修改它
modCount++;
// overflow-conscious code
//当前最小容量 - 实际数组大小大于0. 10 - 0 > 0
//第二次添加,2 - 10 < 0,不进行扩容
if (minCapacity - elementData.length > 0)
//如果elementData的大小不够就扩容
grow(minCapacity);
}
private void grow(int minCapacity) {
// overflow-conscious code
//第一次是0
//第二次扩容10
int oldCapacity = elementData.length;
//第一次是0
//第二次扩容15
int newCapacity = oldCapacity + (oldCapacity >> 1);
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:
//第一次扩容是十个null,使用扩容机制来确定要扩容到多大
elementData = Arrays.copyOf(elementData, newCapacity);
}
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 class Vector<E>
extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
Vector底层也是一个对象数组
protected Object[] elementData;
Vector是线程同步的,即线程安全的,Vector类的操作方法带有synchronized,在开发中需要线程同步安全时,考虑使用Vector
public Vector() {
this(10);
}
public synchronized void addElement(E obj) {
modCount++;
ensureCapacityHelper(elementCount + 1);
elementData[elementCount++] = obj;
}
private void ensureCapacityHelper(int minCapacity) {
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
//扩容两倍 有参构造可以指定capacityIncrement
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);
}
public LinkedList() {
}
此时LinkedList的属性first = null last = null
public boolean add(E e) {
linkLast(e);
return true;
}
Node(Node<E> prev, E element, Node<E> next) {
this.item = element;
this.next = next;
this.prev = prev;
}
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++;
}
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) {
// 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;
}