The List interface implements three classes: ArrayList, Vector, LinkedList.
LinkedList is mainly used to maintain the insertion order of data and adopts the linked list structure.
The main differences between ArrayList and Vector are as follows:
(1) : Vector is thread safe. There are many synchronized in the source code, but ArrayList is not. As a result, Vector efficiency cannot be compared with ArrayList;
(2) : both ArrayList and Vector use linear continuous storage space. When the storage space is insufficient, ArrayList will increase to 50% by default, and Vector will double by default;
(3) : Vector can set capacityIncrement, but ArrayList can't. literally, it is the parameter of capacity, Increment and capacity growth.
Source code analysis:
First look at the constructor:
ArrayList: Three
/** * Constructs an empty list with an initial capacity of ten. * Construct a list with a default initial capacity of 10 */ public ArrayList() { super(); this.elementData = EMPTY_ELEMENTDATA; } /** * Construct a list initialCapacity with a specified default length that cannot be less than 0; * Constructs an empty list with the specified initial capacity. * * @param initialCapacity the initial capacity of the list * @throws IllegalArgumentException if the specified initial capacity * is negative */ public ArrayList(int initialCapacity) { super(); if (initialCapacity < 0) throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity); this.elementData = new Object[initialCapacity]; } /** Construct a list containing the collection element * 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) { ... }
Vector: Four
//Construct a list with a specified default length public Vector(int initialCapacity) { this(initialCapacity, 0); } //Construct a list with a default initial capacity of 10 public Vector() { this(10); } //Construct a list containing the collection element public Vector(Collection<? extends E> c) { ... } //The difference is that capacity increment can be set public Vector(int initialCapacity, int capacityIncrement) { super(); ... }
Vector has an additional public vector (int initial capacity, int capacity increment) constructor, which can set capacity growth. arraylist does not.
ArrayList class:
public boolean add(E e) { ensureCapacityInternal(size + 1); // Increments modCount!! elementData[size++] = e; return true; } private void ensureCapacityInternal(int minCapacity) { modCount++; // overflow-conscious code //If after adding an element, the size of the new container is larger than the capacity of the container, then the value cannot be saved and the space needs to be expanded if (minCapacity - elementData.length > 0) grow(minCapacity); } private void grow(int minCapacity) { // overflow-conscious code int oldCapacity = elementData.length; int newCapacity = oldCapacity + (oldCapacity >> 1); //Increase the space by 50% (i.e. 1.5 times) if (newCapacity - minCapacity < 0) //If the capacity of the container is not enough, simply set minCapacity to the size of the container newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE > 0) //If the expanded container is too large, execute hugeCapacity newCapacity = hugeCapacity(minCapacity); // minCapacity is usually close to size, so this is a win: elementData = Arrays.copyOf(elementData, newCapacity); }
Class Vector:
public synchronized boolean add(E e) { modCount++; ensureCapacityHelper(elementCount + 1); elementData[elementCount++] = e; return true; } 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; int newCapacity = oldCapacity + ((capacityIncrement > 0) ? capacityIncrement : oldCapacity); /** This expansion requires a judgment: if the capacity increment is not initialized to 0, the public Vector(int initialCapacity,int capacityIncrement) is used For initialization by construction method, the capacity of expansion is (old capacity + capacity increment), which is the value of original capacity plus capacity increment; If the capacity increment is not set, the capacity after expansion is (oldCapacity+oldCapacity), twice the original capacity. **/ if (newCapacity - minCapacity < 0) newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE > 0) newCapacity = hugeCapacity(minCapacity); elementData = Arrays.copyOf(elementData, newCapacity); }