先上一张Java集合的框架图,便于参考
以下所有特性仅代表自JAVA 1.8
List
interface Iterable<E>
interface Collection<E> extends Iterable<E>
interface Queue<E> extends Collection<E>
interface Deque<E> extends Queue<E>
interface List<E> extends Collection<E>
abstract AbstractCollection<E> implements Collection<E>
abstract AbstractList<E> extends AbstractCollection<E> implements List<E>
abstract AbstractSequentialList<E> extends AbstractList<E>
ArrayList
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
/**
* Default initial capacity.
*/
private static final int DEFAULT_CAPACITY = 10;
/**
* Shared empty array instance used for empty instances.
*/
private static final Object[] EMPTY_ELEMENTDATA = {};
1.使用对象数组Object[]存储数据,初始化容量为10
2.由于使用对象数组Object[]存储数据, 所以ArrayList并不适合操作(增添,删除)大规模的数据,因为会涉及到扩容(扩容过程中需要新建更大内存的对象)和删除(数据删除会涉及到数据移位)。此处扩容同样使用浅拷贝 详细参考
3.因此ArrayLis仅适合少量的数据操作和大规模数据的遍历操作。
4.线程不安全
LinkedList
public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, java.io.Serializable
{
transient int size = 0;
/**
* Pointer to first node.
* Invariant: (first == null && last == null) ||
* (first.prev == null && first.item != null)
*/
transient Node<E> first;
/**
* Pointer to last node.
* Invariant: (first == null && last == null) ||
* (last.next == null && last.item != null)
*/
transient Node<E> last;
1.使用 Node<E> (双向链表结构)来存储数据
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;
}
}
2.由于使用链表结构存储数据,因此LinkedList适合操作(增删)大规模数据。
3.线程不安全
Vector
public class Vector<E>
extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
/**
* The array buffer into which the components of the vector are
* stored. The capacity of the vector is the length of this array buffer,
* and is at least large enough to contain all the vector's elements.
*
* <p>Any array elements following the last element in the Vector are null.
*
* @serial
*/
protected Object[] elementData;
// default size 10
public Vector() {
this(10);
}
public synchronized void addElement(E obj) {...}
.
.
.
public synchronized void removeAllElements() {...}
1.使用Object数组存储数据,默认初始大小10
2.所有操作都有添加synchronized关键字,因此为线程安全的
Stack
public
class Stack<E> extends Vector<E> {
1.由于Stack继承自Vector,因此用Object数组存储数据,默认初始大小10,并且线程安全的
2. 满足Stack数据结构的特点 FILO