Some time ago, the development task was relatively easy, so I learned how to encapsulate some collection classes. Today I will summarize the similarities and differences between LinkList and ArrayList.
Same point |
We can find that ArrayList and LinkedList are both implementation classes of the interface List, which means that there must be some identical methods in both of them, such as size(), isEmpty(), contains(), add(), remove( ), get(), set(), etc., but their implementation is quite different
difference |
One, the underlying data structure
ArrayList is: Based on the dynamic array structure
LinkedList: Based on the data structure of the linked list
2. Comparison of each performance
The difference in data structure determines the performance of the two has advantages and disadvantages. Random access, ArrayList is more efficient; in general tasks, LinkedList performs better for add and delete operations, but in fact, looking at the source code, you will find that the two are fast and the slow is not simply judged. Let's take a look at the source code of the two methods get, remove and add
ArrayList:
//set
public E set(int index, E e) {
rangeCheck(index);
checkForComodification();
E oldValue = ArrayList.this.elementData(offset + index);
ArrayList.this.elementData[offset + index] = e;
return oldValue;
}
//get
public E get(int index) {
rangeCheck(index);
checkForComodification();
return ArrayList.this.elementData(offset + index);
}
//add
public void add(int index, E element) {
rangeCheckForAdd(index);
ensureCapacityInternal(size + 1); // Increments modCount!!
System.arraycopy(elementData, index, elementData, index + 1,
size - index);
elementData[index] = element;
size++;
}
//remove
public E remove(int index) {
rangeCheck(index);
modCount++;
E oldValue = elementData(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;
}
LinkedList:
//add
public void add(int index, E element) {
checkPositionIndex(index);
if (index == size)
linkLast(element);
else
linkBefore(element, node(index));
}
//set
public E set(int index, E element) {
checkElementIndex(index);
Node<E> x = node(index);
E oldVal = x.item;
x.item = element;
return oldVal;
}
//get
public E get(int index) {
checkElementIndex(index);
return node(index).item;
}
//remove
public E remove(int index) {
checkElementIndex(index);
return unlink(node(index));
}
//节点定位
Node<E> node(int index) {
// assert isElementIndex(index);
if (index < (size >> 1)) {
Node<E> x = first;
for (int i = 0; i < index; i++)
x = x.next;
return x;
} else {
Node<E> x = last;
for (int i = size - 1; i > index; i--)
x = x.prev;
return x;
}
}
We will find that when the array is deleted or added, the System.arraycopy() method is used. This method describes the movement process of the objects in the array, and the node() method is used to add or delete the linkedlist. This method is the positioning of the node, so in general, the speed of the two is not very sure.