java LinkedList源碼詳解及實例
一、LinkedList概述:
LinkedList與ArrayList一樣,是實現(xiàn)了List接口。由于LinkedList是基于鏈表實現(xiàn)的,所以它執(zhí)行插入和刪除操作時比ArrayList更高效,而隨機訪問的性能要比ArrayList低。
二、LinkedList的實現(xiàn):
1、構(gòu)造方法
//構(gòu)造一個空的LinkedList
public LinkedList() {
}
//接收一個Collection參數(shù)c,默認(rèn)構(gòu)造方法構(gòu)造一個空的鏈表,并通過addAll將c中的元素全部添加到鏈表中。
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
2、一些方法
2.1 getFirst()
//獲取LinkedList第一個元素,如果LinkedList為空,則拋出異常。
public E getFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return f.item;
}
2.2 getLast()
//獲取getLast()最后一個元素,如果LinkedList為空,則拋出異常。
public E getLast() {
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
return l.item;
}
2.3 removeFirst()
//刪除LinkedList第一個元素,如果LinkedList為空,則拋出異常。
public E removeFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return unlinkFirst(f);
}
2.4 removeLast()
//刪除LinkedList最后一個元素,如果LinkedList為空,則拋出異常。
public E removeLast() {
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
return unlinkLast(l);
}
2.5 addFirst(E e)
//在LinkedList開始的位置插入一個新元素。
public void addFirst(E e) {
linkFirst(e);
}
2.6 addLast(E e)
//在LinkedList末尾的位置插入一個新的元素。
public void addLast(E e) {
linkLast(e);
}
2.7 contains(Object o)
//判斷LinkedList中是否包含某個元素,若包含返回True,否則返回False
public boolean contains(Object o) {
return indexOf(o) != -1;
}
2.8 add(E e)
//在LinkedList末尾處添加一個新的元素。
public boolean add(E e) {
linkLast(e);
return true;
}
2.9 remove(Object o)
//刪除LinkedList中第一個出現(xiàn)的指定元素,如果LinkedList中不包含該元素,則鏈表保持不變。
public boolean remove(Object o) {
if (o == null) {
for (Node<E> x = first; x != null; x = x.next) {
if (x.item == null) {
unlink(x);
return true;
}
}
} else {
for (Node<E> x = first; x != null; x = x.next) {
if (o.equals(x.item)) {
unlink(x);
return true;
}
}
}
return false;
}
2.10 addAll()
//增加Collection中的所有元素,
public boolean addAll(Collection<? extends E> c) {
return addAll(size, c);
}
//index參數(shù)指定collection中插入的第一個元素的位置
public boolean addAll(int index, Collection<? extends E> c) {
checkPositionIndex(index);
Object[] a = c.toArray();
int numNew = a.length;
if (numNew == 0)
return false;
Node<E> pred, succ;
if (index == size) {
succ = null;
pred = last;
} else {
succ = node(index);
pred = succ.prev;
}
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;
}
if (succ == null) {
last = pred;
} else {
pred.next = succ;
succ.prev = pred;
}
size += numNew;
modCount++;
return true;
}
2.11 clear()
//刪除LinkedList中的所有元素。
public void clear() {
//刪除LinkedList中所有鏈接是沒有必要的,但是:
// 可以幫助分代GC,如果刪除節(jié)點超過一代就會釋放內(nèi)存,因為有一個可用的迭代器。
for (Node<E> x = first; x != null; ) {
Node<E> next = x.next;
x.item = null;
x.next = null;
x.prev = null;
x = next;
}
first = last = null;
size = 0;
modCount++;
}
2.12 get(int index)
//獲取指定位置的節(jié)點
public E get(int index) {
checkElementIndex(index);
return node(index).item;
}
2.13 set(int index, E element)
//給指定節(jié)點賦一個指定的值,替換原來的值。
public E set(int index, E element) {
checkElementIndex(index);
Node<E> x = node(index);
E oldVal = x.item;
x.item = element;
return oldVal;
}
2.14 add(int index, E element)
//在指定位置插入一個指定的值,原來該位置上以后的節(jié)點依次向后移動一位。
public void add(int index, E element) {
checkPositionIndex(index);
if (index == size)
linkLast(element);
else
linkBefore(element, node(index));
}
2.15 remove(int index)
//刪除指定位置的值,該位置之后的節(jié)點依次向前移動一位。
public E remove(int index) {
checkElementIndex(index);
return unlink(node(index));
}
三、總結(jié)
LinkedList,通俗的理解就是數(shù)據(jù)結(jié)構(gòu)中講的鏈表在Java語言中的實現(xiàn),所以在鏈表中所有操作,LinkedList中都有,其實現(xiàn)原理也都是基于數(shù)據(jù)結(jié)構(gòu)中所講的對鏈表中的節(jié)點插入、刪除、移動等方法。
LinkedList 是一個繼承于AbstractSequentialList的雙向鏈表。它也可以被當(dāng)作堆棧、隊列或雙端隊列進行操作。
LinkedList 實現(xiàn) List 接口,能對它進行隊列操作。
LinkedList 實現(xiàn) Deque 接口,即能將LinkedList當(dāng)作雙端隊列使用。
LinkedList 實現(xiàn)了Cloneable接口,即覆蓋了函數(shù)clone(),能克隆。
LinkedList 實現(xiàn)java.io.Serializable接口,這意味著LinkedList支持序列化,能通過序列化去傳輸。
LinkedList 是非同步的。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
java基于C/S結(jié)構(gòu)實現(xiàn)多線程聊天室
這篇文章主要為大家詳細(xì)介紹了java基于C/S結(jié)構(gòu)實現(xiàn)多線程聊天室,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-01-01
Hystrix?Dashboard斷路監(jiān)控儀表盤的實現(xiàn)詳細(xì)介紹
這篇文章主要介紹了Hystrix?Dashboard斷路監(jiān)控儀表盤的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-09-09
Spring Security 實現(xiàn)“記住我”功能及原理解析
這篇文章主要介紹了Spring Security 實現(xiàn)“記住我”功能及原理解析,需要的朋友可以參考下2020-05-05

