LinkedList 認識
LinkedList是一種雙向鏈表藏雏,雙向鏈表我認為有兩點含義:
鏈表中任意一個存儲單元都可以通過向前或者向后尋址的方式獲取到其前一個存儲單元和其后一個存儲單元
鏈表的尾節(jié)點的后一個節(jié)點是鏈表的頭結(jié)點,鏈表的頭結(jié)點的前一個節(jié)點是鏈表的尾節(jié)點
關注點 | 結(jié)論 |
---|---|
LinkedList是否允許空 | 允許 |
LinkedList是否允許重復數(shù)據(jù) | 允許 |
LinkedList是否有序 | 有序 |
LinkedList是否線程安全 | 非線程安全 |
LinkedList 有個內(nèi)部類:
E item::代表的存儲的元素
Node<E> next:代表鏈表的下一個節(jié)點
Node<E> prev:代表鏈表的上一個節(jié)點
LinkedList 構(gòu)造器
public LinkedList() {
}
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
public boolean addAll(int index, Collection<? extends E> c) {
//檢查index 值是否合法
checkPositionIndex(index);
//轉(zhuǎn)化為數(shù)組
Object[] a = c.toArray();
//獲取數(shù)組的長度
int numNew = a.length;
if (numNew == 0)
return false;
Node<E> pred, succ;
if (index == size) {
succ = null;
//前一個節(jié)點就是 集合的最后一個節(jié)點
pred = last;
} else {
//假如集合的索引index上存在節(jié)點作煌,那么 succ 就為 索引 index 上的節(jié)點
succ = node(index);
//前一個節(jié)點為 succ 節(jié)點的上一個節(jié)點
pred = succ.prev;
}
//循環(huán)遍歷 Object[] 數(shù)組
for (Object o : a) {
@SuppressWarnings("unchecked") E e = (E) o;
// new 一個節(jié)點 該節(jié)點的前一個節(jié)點為 pred 節(jié)點掘殴,pred 已在上面 得到了
Node<E> newNode = new Node<>(pred, e, null);
//假如pred 節(jié)點為空
if (pred == null)
// 那么第一個節(jié)點就位 newNode 節(jié)點,比如在剛開始 集合為空的情況下
first = newNode;
else
// pred 的下一個節(jié)點 為 newNode
pred.next = newNode;
//pred 節(jié)點指向 newNode 節(jié)點
pred = newNode;
}
if (succ == null) {
// 集合的最后節(jié)點為 pred
last = pred;
} else {
pred.next = succ;
succ.prev = pred;
}
//更新 集合的 大小
size += numNew;
//修改次數(shù)自增
modCount++;
return true;
}
LinkedList 添加元素
public boolean add(E e) {
linkLast(e);
return true;
}
public void add(int index, E element) {
checkPositionIndex(index);
if (index == size)
linkLast(element);
else
linkBefore(element, node(index));
}
public boolean addAll(Collection<? extends E> c) {
return addAll(size, c);
}
private void linkLast(E e) {
//將 節(jié)點 l 指向 last 最后一個節(jié)點
final Node<E> l = last;
// new 一個節(jié)點 該節(jié)點的上一個節(jié)點為 l 也就是最后一個節(jié)點
final Node<E> newNode = new Node<>(l, e, null);
//將最后一個節(jié)點 last 指向 newNode 節(jié)點
last = newNode;
if (l == null)
//假如 l 節(jié)點為空 粟誓,比如在 集合為空 的情況下 last 節(jié)點就位null
first = newNode;
else
// 將 l 節(jié)點的下一個節(jié)點指向 newNode 節(jié)點
l.next = newNode;
//集合 大小自增
size++;
//修改次數(shù)自增
modCount++;
}
//原理 跟 linkLast 差不多
private void linkFirst(E e) {
final Node<E> f = first;
final Node<E> newNode = new Node<>(null, e, f);
first = newNode;
if (f == null)
last = newNode;
else
f.prev = newNode;
size++;
modCount++;
}
void linkBefore(E e, Node<E> succ) {
// pred 指向 succ 節(jié)點的上一個節(jié)點
final Node<E> pred = succ.prev;
// new 一個新節(jié)點 該節(jié)點 上一個節(jié)點指向 pred 節(jié)點杯巨,下一個幾點指向 succ 節(jié)點
final Node<E> newNode = new Node<>(pred, e, succ);
//succ 的上一個節(jié)點指向 newNode 節(jié)點
succ.prev = newNode;
if (pred == null)
first = newNode;
else
// pred 下一個節(jié)點指向 newNode 節(jié)點
pred.next = newNode;
size++;
//修改次數(shù)自增
modCount++;
}
LinkedList 查看元素
public E get(int index) {
checkElementIndex(index);
return node(index).item;
}
Node<E> node(int index) {
//當index小于數(shù)組大小的一半的時候(size >> 1表示size / 2,
//使用移位運算提升代碼運行效率)努酸,向后查找服爷;否則,向前查找获诈。
if (index < (size >> 1)) {
Node<E> x = first;
for (int i = 0; i < index; i++)
//不斷遍歷當前節(jié)點的下一個節(jié)點
x = x.next;
return x;
} else {
Node<E> x = last;
for (int i = size - 1; i > index; i--)
x = x.prev;
return x;
}
}
LinkedList 刪除元素
//移除指定索引的 元素
public E remove(int index) {
checkElementIndex(index);
return unlink(node(index));
}
//移除第一個元素
public E remove() {
return removeFirst();
}
//移除最有一個元素
public E removeLast() {
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
return unlinkLast(l);
}
//移除第一個元素
public E removeFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return unlinkFirst(f);
}
E unlink(Node<E> x) {
// 獲取節(jié)點 x 的元素
final E element = x.item;
//獲取節(jié)點 x 的下一個節(jié)點
final Node<E> next = x.next;
//獲取節(jié)點 x 的上一個節(jié)點
final Node<E> prev = x.prev;
if (prev == null) {
first = next;
} else {
//將 節(jié)點 x 的上一個 節(jié)點的下一個節(jié)點 指向節(jié)點 x 的下一個節(jié)點
prev.next = next;
//將節(jié)點 x 上一個節(jié)點 置為 null 讓虛擬機可以回收這個節(jié)點
x.prev = null;
}
if (next == null) {
last = prev;
} else {
// 將 節(jié)點 x 的下一個節(jié)點的上一個節(jié)點 指向 節(jié)點 x 的上一個節(jié)點
next.prev = prev;
//將節(jié)點 x 下一個節(jié)點 置為 null 讓虛擬機可以回收這個節(jié)點
x.next = null;
}
//將 節(jié)點 x 的元素置為 null
x.item = null;
size--;
modCount++;
return element;
}
LinkedList和ArrayList的對比
- 順序插入速度ArrayList會比較快仍源,因為ArrayList是基于數(shù)組實現(xiàn)的,數(shù)組是事先new好的舔涎,只要往指定位置塞一個數(shù)據(jù)就好了笼踩;LinkedList則不同,每次順序插入的時候LinkedList將new一個對象出來亡嫌,如果對象比較大嚎于,那么new的時間勢必會長一點掘而,再加上一些引用賦值的操作,所以順序插入LinkedList必然慢于ArrayList
- 因為LinkedList里面不僅維護了待插入的元素于购,還維護了Entry的前置Entry和后繼Entry袍睡,如果一個LinkedList中的Entry非常多,那么LinkedList將比ArrayList更耗費一些內(nèi)存
- 數(shù)據(jù)遍歷的速度肋僧,看最后一部分斑胜,這里就不細講了,結(jié)論是:使用各自遍歷效率最高的方式嫌吠,ArrayList的遍歷效率會比LinkedList的遍歷效率高一些
- 有些說法認為LinkedList做插入和刪除更快止潘,這種說法其實是不準確的:
(1)LinkedList做插入、刪除的時候辫诅,慢在尋址凭戴,快在只需要改變前后Entry的引用地址
(2)ArrayList做插入、刪除的時候炕矮,慢在數(shù)組元素的批量copy簇宽,快在尋址
所以,如果待插入吧享、刪除的元素是在數(shù)據(jù)結(jié)構(gòu)的前半段尤其是非常靠前的位置的時候譬嚣,LinkedList的效率將大大快過ArrayList钢颂,因為ArrayList將批量copy大量的元素;越往后拜银,對于LinkedList來說殊鞭,因為它是雙向鏈表,所以在第2個元素后面插入一個數(shù)據(jù)和在倒數(shù)第2個元素后面插入一個元素在效率上基本沒有差別尼桶,但是ArrayList由于要批量copy的元素越來越少操灿,操作速度必然追上乃至超過LinkedList。
從這個分析看出泵督,如果你十分確定你插入趾盐、刪除的元素是在前半段,那么就使用LinkedList小腊;如果你十分確定你刪除救鲤、刪除的元素在比較靠后的位置,那么可以考慮使用ArrayList秩冈。如果你不能確定你要做的插入本缠、刪除是在哪兒呢?那還是建議你使用LinkedList吧入问,因為一來LinkedList整體插入丹锹、刪除的執(zhí)行效率比較穩(wěn)定稀颁,沒有ArrayList這種越往后越快的情況;二來插入元素的時候楣黍,弄得不好ArrayList就要進行一次擴容匾灶,記住,ArrayList底層數(shù)組擴容是一個既消耗時間又消耗空間的操作锡凝。
對LinkedList以及ArrayList的迭代
ArrayList使用最普通的for循環(huán)遍歷比較快粘昨,LinkedList使用foreach循環(huán)比較快,看一下兩個List的定義:
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, java.io.Serializable
ArrayList 實現(xiàn)了 RandomAccess 標記接口 窜锯,對于 RandomAccess 標記接口张肾,JDK文檔中 有描述:
寫一個程序測試下:
public class ListTest {
private static final int SIZE = 100000;
public static void loopList(List<String> list){
long startTime = System.currentTimeMillis();
for(int i = 0; i < list.size(); i++){
list.get(i);
}
System.out.println(list.getClass().getSimpleName() + "普通循環(huán)時間:" + (System.currentTimeMillis() - startTime) + "ms");
startTime = System.currentTimeMillis();
for(String str : list){
}
System.out.println(list.getClass().getSimpleName() + "foreach循環(huán)時間:" + (System.currentTimeMillis() - startTime) + "ms");
}
public static void main(String[] args) {
List<String> arrayList = new ArrayList<String>(SIZE);
for(int i = 0; i < SIZE ;i++){
arrayList.add(i + "");
}
List<String> linkedList = new LinkedList<String>();
for(int i = 0; i < SIZE ;i++){
linkedList.add(i + "");
}
loopList(arrayList);
loopList(linkedList);
}
}
結(jié)果為:
ArrayList 普通循環(huán)時間:0ms
ArrayList foreach循環(huán)時間:15ms
LinkedList 普通循環(huán)時間:31918ms
LinkedList foreach循環(huán)時間:11ms