LinkedList簡介
- LinkedList基于雙向鏈表實現(xiàn)
- LinkedList相對于Arraylist來說,get和set等隨機(jī)訪問會比較慢,LinkedList需要移動指針框弛;add和remove會比較快荣刑。
- LinkedList類還為在列表開頭和結(jié)尾的get症杏,remove,insert元素提供統(tǒng)一的命名方法搅吁,這些操作允許將鏈表用做堆棧、隊列或者雙端隊列虽界。此類實現(xiàn) Deque 接口炫加,為 add窟哺、poll 提供先進(jìn)先出隊列操作泻轰,以及其他堆棧和雙端隊列操作。
- 非線程安全且轨,不同步浮声。
定義
LinkedList集成AbstractSequentialList,實現(xiàn)了List旋奢,Deque泳挥,Cloneable,Serializable接口至朗。AbstractSequentialList提供了骨干實現(xiàn)屉符。Deque一個線性 collection,支持在兩端插入和移除元素锹引,定義了雙端隊列的操作矗钟。
源碼分析
jdk1.7.0_71
//節(jié)點個數(shù)
transient int size = 0;
//前驅(qū)節(jié)點
transient Node<E> first;
//后繼節(jié)點
transient Node<E> last;
無參構(gòu)造
public LinkedList() {}
根據(jù)其他容器進(jìn)行構(gòu)造
public LinkedList(Collection<? extends E> c) {}
getFirst()/getLast() 獲取第一個/獲取最后一個
public E getFirst() {}
public E getLast() {}
removeFirst()/removeLast() 刪除第一個/刪除最后一個
public E removeFirst() {}
public E removeLast() {}
addFirst(E e)/addLast(E e) 添加到頭/尾
public void addFirst(E e) {}
public void addLast(E e) {}
是否包含指定的元素
public boolean contains(Object o) {
return indexOf(o) != -1;
}
指定元素的位置索引
public int indexOf(Object o) {}
指定元素的最后的位置索引
public int lastIndexOf(Object o) {}
list的大小
public int size() {
return size;
}
add() 添加到末尾
public boolean add(E e) {}
remove(Object o)/removeFirstOccurrence(Object o) 移除第一個o
public boolean remove(Object o) {}
public boolean removeFirstOccurrence(Object o){}
addAll(Collection<? extends E> c) 添加到list結(jié)尾
public boolean addAll(Collection<? extends E> c) {}
addAll(int index, Collection<? extends E> c) 指定的位置以后添加全部
public boolean addAll(int index, Collection<? extends E> c) {}
clear() 清空
public void clear() {}
get(int index) 獲取指定位置的元素
public E get(int index) {}
指定位置替換成新元素,返回舊元素
public E set(int index, E element) {}
add(int index, E element)指定位置插入指定元素
public void add(int index, E element) {}
remove(int index) 移除指定位置的元素
public E remove(int index) {}
peek()/peekFirst() 獲取list頭
public E peek() {}
public E peekFirst() {}
element() 獲取list頭
public E element() {}
poll()/pollFirst() 獲取list頭,并刪除
public E poll() {}
public E pollFirst() {}
remove() 刪除第一個
public E remove() {}
offer(E e)/offerLast(E e) 添加e到尾部
public boolean offer(E e) {}
public boolean offerLast(E e) {}
offerFirst(E e)/push(E e) 添加e到頭部
public boolean offerFirst(E e) {}
public void push(E e){}
peekLast() 獲取list尾
public E peekLast() {}
pollLast() 獲取list尾,并刪除
public E pollLast
pop() 刪除頭
public E pop(){}
removeLastOccurrence(Object o)從后面開始刪除第一個匹配的元素
public boolean removeLastOccurrence(Object o) {}
listIterator(int index)從指定的位置開始返回一個listIterator
public ListIterator<E> listIterator(int index) {}
descendingIterator() 逆序迭代器
public Iterator<E> descendingIterator() {
//內(nèi)部類
return new DescendingIterator();
}
clone() 淺拷貝
public Object clone() {}
toArray() 轉(zhuǎn)換成Object數(shù)組
public Object[] toArray() {}
toArray(T[] a) 轉(zhuǎn)換成指定類型的數(shù)組
public <T> T[] toArray(T[] a) {
if (a.length < size)
a = (T[])java.lang.reflect.Array.newInstance(
a.getClass().getComponentType(), size);
int i = 0;
Object[] result = a;
for (Node<E> x = first; x != null; x = x.next)
result[i++] = x.item;
if (a.length > size)
a[size] = null;
return a;
}