聊一聊LinkedList的特點吧~(以下都是基於jdk1.8)
繼承樹 |
LinkedList的繼承樹如下圖:
特點 |
(1)允許null值
(2)內部以雙向鏈表的形式來保存集合中的元素
/**
* 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;
這個節點是一個雙向節點:
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;
}
}
(3)實現了Deque接口,意味着可以當做雙端隊列、棧來使用
(4)線程不安全,線程安全的可以使用:List list = Collections.synchronizedList(new LinkedList(…));
(5)fail-fast機制
(6)所有指定位置的操作都是從頭開始遍歷進行的
(7)元素是有序的,輸出順序與輸入順序一致