The differences between ArrayList and LinkedList are as follows:
- ArrayList is a data structure based on a dynamic array , and LinkedList is a data structure based on a linked list ;
- For random access get and set, ArrayList LinkedList superior , because LinkedList to move the pointer;
- For the add and remove operations, generally everyone will say that LinkedList is faster than ArrayList because ArrayList needs to move data. But the reality is not so, for adding or deleting, LinkedList and ArrayList are not clear about who fast who slow
(not elaborate, specifically to see this blog )
ArrayList common methods
boolean |
add(E e) Append the specified element to the end of this list. |
---|---|
void |
add(int index, E element) Insert the specified element at the specified position in this list. |
boolean |
addAll(Collection<? extends E> c) Appends all elements in the specified collection to the end of this list in the order returned by the Iterator of the specified collection. |
void |
clear() Remove all elements from the list. |
Object |
clone() Returns a ArrayList shallow copy instance. |
boolean |
contains(Object o) If this list contains the specified element, it is returned true . |
void |
forEach(Consumer<? super E> action) For Iterable each element to perform a given operation, until all elements have been processed or an operation exception is thrown. |
E |
get(int index) Returns the element at the specified position in this list. |
int |
indexOf(Object o) Returns the index of the first occurrence of the specified element in this list, or -1 if this list contains no elements. |
boolean |
isEmpty() If this list contains no elements, it returns true . |
Iterator<E> |
iterator() Returns an iterator of the elements in this list in the correct order. |
int |
lastIndexOf(Object o) Returns the index of the last occurrence of the specified element in this list, or -1 if this list contains no elements. |
E |
remove(int index) Delete the element at the specified position in the list. |
boolean |
remove(Object o) Remove the first occurrence of the specified element from the list (if it exists). |
boolean |
removeAll(Collection<?> c) Remove all elements contained in the specified set from this list. |
void |
replaceAll(UnaryOperator<E> operator) Replace each element of the list with the result of applying the operator to that element. |
boolean |
retainAll(Collection<?> c) Only keep the elements contained in the specified set in this list. |
E |
set(int index, E element) Replace the element at the specified position in this list with the specified element. |
int |
size() Returns the number of elements in this list. |
void |
sort(Comparator<? super E> c) Use the provided Comparator list can be sorted in order to compare elements. |
List<E> |
subList(int fromIndex, int toIndex) Returns the specified list fromIndex (including) and toIndex exclusive views between. |
Object[] |
toArray() Return an array containing all the elements in this list in the correct order (from first to last element). |
<T> T[] |
toArray(T[] a) Returns an array containing all the elements in this list in the correct order (from the first to the last element); the runtime type of the returned array is the runtime type of the specified array. |
LinkedList common methods
boolean |
add(E e) Append the specified element to the end of this list. |
---|---|
void |
add(int index, E element) Insert the specified element at the specified position in this list. |
boolean |
addAll(Collection<? extends E> c) Appends all elements in the specified collection to the end of this list in the order returned by the iterator of the specified collection. |
boolean |
addAll(int index, Collection<? extends E> c) Insert all elements in the specified collection into this list, starting from the specified position. |
void |
addFirst(E e) Insert the specified element at the beginning of the list. |
void |
addLast(E e) Append the specified element to the end of this list. |
void |
clear() Remove all elements from the list. |
Object |
clone() Returns a LinkedList shallow version. |
boolean |
contains(Object o) If this list contains the specified element, it is returned true . |
Iterator<E> |
descendingIterator() Returns an iterator of the elements in this deque in reverse order. |
E |
get(int index) Returns the element at the specified position in this list. |
E |
getFirst() Returns the first element in this list. |
E |
getLast() Returns the last element in this list. |
int |
indexOf(Object o) Returns the index of the first occurrence of the specified element in this list, or -1 if this list contains no elements. |
int |
lastIndexOf(Object o) Returns the index of the last occurrence of the specified element in this list, or -1 if this list contains no elements. |
boolean |
offer(E e) Add the specified element to the end (the last element) of this list. |
boolean |
offerFirst(E e) Insert the specified element at the front of this list. |
boolean |
offerLast(E e) Insert the specified element at the end of the list. |
E |
peek() Retrieve but not delete the head (first element) of this list. |
E |
peekFirst() Retrieve but do not delete the first element of this list, and return if this list is empty null . |
E |
peekLast() Retrieve but not delete the last element of this list, and return if this list is empty null . |
E |
poll() 检索并删除此列表的头(第一个元素)。 |
E |
pollFirst() 检索并删除此列表的第一个元素,如果此列表为空,则返回 null 。 |
E |
pollLast() 检索并删除此列表的最后一个元素,如果此列表为空,则返回 null 。 |
E |
pop() 从此列表表示的堆栈中弹出一个元素。 |
void |
push(E e) 将元素推送到由此列表表示的堆栈上。 |
E |
remove() 检索并删除此列表的头(第一个元素)。 |
E |
remove(int index) 删除该列表中指定位置的元素。 |
boolean |
remove(Object o) 从列表中删除指定元素的第一个出现(如果存在)。 |
E |
removeFirst() 从此列表中删除并返回第一个元素。 |
boolean |
removeFirstOccurrence(Object o) 删除此列表中指定元素的第一个出现(从头到尾遍历列表时)。 |
E |
removeLast() 从此列表中删除并返回最后一个元素。 |
boolean |
removeLastOccurrence(Object o) 删除此列表中指定元素的最后一次出现(从头到尾遍历列表时)。 |
E |
set(int index, E element) 用指定的元素替换此列表中指定位置的元素。 |
int |
size() 返回此列表中的元素数。 |
Object[] |
toArray() 以正确的顺序(从第一个到最后一个元素)返回一个包含此列表中所有元素的数组。 |
<T> T[] |
toArray(T[] a) 以正确的顺序返回一个包含此列表中所有元素的数组(从第一个到最后一个元素); 返回的数组的运行时类型是指定数组的运行时类型。 |