Have you learned about those List collections?
ArrayList、LinkedList 、Vector
Talk about ArrayList
The bottom layer of ArrayList is an array.
There are three types of constructors:
① No-parameter construction, initialize an array with a default size of 10.
② Initialize a capacity to this size.
③ Given another existing set A, call A's toArray method. If A is 0, then it will be treated as a parameterless construction.
If there are elements in A, then call the Array.copyOf function to copy it.
Add is the same as an ordinary array, with more judgments and operations for expansion.
Judge the expansion before the real add
//1.5倍扩容 jdk 1.8
int newCapacity = oldCapacity + (oldCapacity >> 1);
If you use the index add, then the native mobile array method will be called
public static native void arraycopy(Object src, int srcPos,
Object dest, int destPos,
int length);
The characteristic is that random access is fast and insertion in the middle is inefficient.
LinkedList also talk about how the bottom layer is implemented
LinkedList is a doubly linked list,
initialized in two ways:
① empty structure
② use an existing collection. Call the addAll method to first toArray into an Object array and then perform tail interpolation. Before inserting, it is forced to convert the generic type given by LinkedList.
Tail plug method
for (Object o : a) {
@SuppressWarnings("unchecked") E e = (E) o;
Node<E> newNode = new Node<>(pred, e, null);
if (pred == null)
first = newNode;
else
pred.next = newNode;
pred = newNode;
}
In the head and tail of the get element and delete if it does not exist, an exception will be thrown.
If it is get an index, it will enter a judgment method first, and it will throw an exception if it is not in the range.
public E getFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return f.item;
}
/**
* Returns the last element in this list.
*
* @return the last element in this list
* @throws NoSuchElementException if this list is empty
*/
public E getLast() {
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
return l.item;
}
/**
* Removes and returns the first element from this list.
*
* @return the first element from this list
* @throws NoSuchElementException if this list is empty
*/
public E removeFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return unlinkFirst(f);
}
/**
* Removes and returns the last element from this list.
*
* @return the last element from this list
* @throws NoSuchElementException if this list is empty
*/
public E removeLast() {
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
return unlinkLast(l);
}
Four add methods:
①add an element: call the tail interpolation method
②addfirst method: call the head interpolation method
③addlast method: call the tail interpolation method
④add index, key: first judge the range, throw an exception if the range is exceeded, and then see if it is the last position, if If yes, call the tail interpolation method directly, otherwise, just look for it.
In the case of offer, the add method is called, the end is inserted, and a true is returned at the end.
Push is to call addFirst and pop to call removeFirst, both of which operate on the head
What about Vector, is it thread-safe?
Yes, almost all methods use the synchronized keyword
How does Vector expand?
The expansion is doubled, if not specified, the array will be migrated