LinkedList源碼淺析(jdk1.8)
阿新 • • 發佈:2017-06-03
指定元素 首部 urn erl ransient 操作 創建 rac rst
LinkedList由雙向鏈表實現的集合,因此可以從頭或尾部雙向循環遍歷。
LinkedList的操作都是對雙向鏈表的操作,理解雙向鏈表的數據結構就很容易理解LinkedList的實現。
雙向鏈表由帶前驅和後繼的節點構成,簡易如下:
如果添加一個c節點,簡易步驟如下:
先創建一個新節點。然後把原先的last節點的next指向新節點,在把新節點的pre指向原先的last,最後新節點指為last節點。
具體的雙向鏈表還是參考數據結構的資料。
一、構造方法
1、無參的
public LinkedList() { }View Code
2、集合為參數的
publicView CodeLinkedList(Collection<? extends E> c) { this(); addAll(c); } public boolean addAll(Collection<? extends E> c) { return addAll(size, c); } 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; } private void checkPositionIndex(int index) { if (!isPositionIndex(index)) throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); } private boolean isPositionIndex(int index) { return index >= 0 && index <= size; }
二、添加元素
1、add(E e)方法,添加節點到尾部
public boolean add(E e) { linkLast(e); return true; } void linkLast(E e) { // 尾節點賦值給l final Node<E> l = last; // 構造新節點 final Node<E> newNode = new Node<>(l, e, null); // 新節點賦給尾節點 last = newNode; if (l == null) first = newNode; else l.next = newNode; size++; modCount++; }View Code
2、addFirst(E e)方法,添加節點到首部
public void addFirst(E e) { linkFirst(e); } 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++; }View Code
三、刪除元素
1、remove(Object o)方法,刪除o節點(只刪除一次),循環鏈表找到要刪除的節點,然後調用unlink()方法進行刪除。
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; } E unlink(Node<E> x) { // assert x != null; final E element = x.item; final Node<E> next = x.next; final Node<E> prev = x.prev; if (prev == null) { first = next; } else { prev.next = next; x.prev = null; } if (next == null) { last = prev; } else { next.prev = prev; x.next = null; } x.item = null; size--; modCount++; return element; }View Code
2、removeFirst()方法,刪除首節點,為空則拋異常。
public E removeFirst() { final Node<E> f = first; if (f == null) throw new NoSuchElementException(); return unlinkFirst(f); } private E unlinkFirst(Node<E> f) { // assert f == first && f != null; final E element = f.item; final Node<E> next = f.next; f.item = null; f.next = null; // help GC first = next; if (next == null) last = null; else next.prev = null; size--; modCount++; return element; }View Code
四、其他方法源碼
public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable, java.io.Serializable { transient int size = 0; /** * 首節點 */ transient Node<E> first; /** * 尾節點 */ transient Node<E> last; /** * 無參構造 */ public LinkedList() { } /** * 有參構造 */ public LinkedList(Collection<? extends E> c) { this(); addAll(c); } /** * 從頭部添加元素(私有的,內部使用) */ 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 linkLast(E e) { final Node<E> l = last; final Node<E> newNode = new Node<>(l, e, null); last = newNode; if (l == null) first = newNode; else l.next = newNode; size++; modCount++; } /** * 在指定元素前插入元素 */ void linkBefore(E e, Node<E> succ) { // assert succ != null; final Node<E> pred = succ.prev; final Node<E> newNode = new Node<>(pred, e, succ); succ.prev = newNode; if (pred == null) first = newNode; else pred.next = newNode; size++; modCount++; } /** * 刪除首節點(私有的,內部使用) */ private E unlinkFirst(Node<E> f) { // assert f == first && f != null; final E element = f.item; final Node<E> next = f.next; f.item = null; f.next = null; // help GC first = next; if (next == null) last = null; else next.prev = null; size--; modCount++; return element; } /** * 刪除尾節點(私有的,內部使用) */ private E unlinkLast(Node<E> l) { // assert l == last && l != null; final E element = l.item; final Node<E> prev = l.prev; l.item = null; l.prev = null; // help GC last = prev; if (prev == null) first = null; else prev.next = null; size--; modCount++; return element; } /** * 刪除節點x */ E unlink(Node<E> x) { // assert x != null; final E element = x.item; final Node<E> next = x.next; final Node<E> prev = x.prev; if (prev == null) { first = next; } else { prev.next = next; x.prev = null; } if (next == null) { last = prev; } else { next.prev = prev; x.next = null; } x.item = null; size--; modCount++; return element; } /** * 獲取首節點 */ public E getFirst() { final Node<E> f = first; if (f == null) throw new NoSuchElementException(); return f.item; } /** * 獲取尾節點 */ public E getLast() { final Node<E> l = last; if (l == null) throw new NoSuchElementException(); return l.item; } /** * 刪除首節點並返回元素值 */ public E removeFirst() { final Node<E> f = first; if (f == null) throw new NoSuchElementException(); return unlinkFirst(f); } /** * 刪除尾節點並返回元素值 */ public E removeLast() { final Node<E> l = last; if (l == null) throw new NoSuchElementException(); return unlinkLast(l); } /** * 把e設為首節點 */ public void addFirst(E e) { linkFirst(e); } /** * 把e設為尾節點 */ public void addLast(E e) { linkLast(e); } /** * 判斷o是否在集合中 */ public boolean contains(Object o) { return indexOf(o) != -1; } /** * 集合大小 */ public int size() { return size; } /** * 添加元素到鏈表尾部 */ public boolean add(E e) { linkLast(e); return true; } /** * 刪除o節點 */ 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; } /** * 添加集合 */ public boolean addAll(Collection<? extends E> c) { return addAll(size, c); } /** * 指定位置添加集合 */ 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; } /** * 清除集合所有元素 */ public void clear() { // Clearing all of the links between nodes is "unnecessary", but: // - helps a generational GC if the discarded nodes inhabit // more than one generation // - is sure to free memory even if there is a reachable Iterator 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++; } // Positional Access Operations /** * 獲取指定位置的元素的值 */ public E get(int index) { checkElementIndex(index); return node(index).item; } /** * 取代指定位置的元素並返回原元素的值 */ public E set(int index, E element) { checkElementIndex(index); Node<E> x = node(index); E oldVal = x.item; x.item = element; return oldVal; } /** * 添加元素到指定位置並後移原先的元素,如果指定的位置是最後,則直接添加元素在尾部 */ public void add(int index, E element) { checkPositionIndex(index); if (index == size) linkLast(element); else linkBefore(element, node(index)); } /** * 刪除指定位置的元素 */ public E remove(int index) { checkElementIndex(index); return unlink(node(index)); } /** * 用於判斷元素索引是否正確 */ private boolean isElementIndex(int index) { return index >= 0 && index < size; } /** * 用於判斷位置索引是否正確 */ private boolean isPositionIndex(int index) { return index >= 0 && index <= size; } /** * 越界異常提示 */ private String outOfBoundsMsg(int index) { return "Index: "+index+", Size: "+size; } private void checkElementIndex(int index) { if (!isElementIndex(index)) throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); } private void checkPositionIndex(int index) { if (!isPositionIndex(index)) throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); } /** * 返回指定位置的節點. */ Node<E> node(int index) { // assert isElementIndex(index); if (index < (size >> 1)) { Node<E> x = first; for (int i = 0; i < index; i++) x = x.next; return x; } else { Node<E> x = last; for (int i = size - 1; i > index; i--) x = x.prev; return x; } } // Search Operations /** * 從頭循環返回第一次出現o的index */ public int indexOf(Object o) { int index = 0; if (o == null) { for (Node<E> x = first; x != null; x = x.next) { if (x.item == null) return index; index++; } } else { for (Node<E> x = first; x != null; x = x.next) { if (o.equals(x.item)) return index; index++; } } return -1; } /** * 從尾循環返回第一次出現o的index */ public int lastIndexOf(Object o) { int index = size; if (o == null) { for (Node<E> x = last; x != null; x = x.prev) { index--; if (x.item == null) return index; } } else { for (Node<E> x = last; x != null; x = x.prev) { index--; if (o.equals(x.item)) return index; } } return -1; } // Queue operations. /** * 返回首節點的值,但不刪除,null也可以返回 */ public E peek() { final Node<E> f = first; return (f == null) ? null : f.item; } /** * 返回首節點的值,但不刪除,如果是null則拋NoSuchElementException異常 */ public E element() { return getFirst(); } /** * 返回首節點並刪除 */ public E poll() { final Node<E> f = first; return (f == null) ? null : unlinkFirst(f); } /** * 返回首節點並刪除,如果首節點為null拋異常 */ public E remove() { return removeFirst(); } /** * 添加元素到尾節點 */ public boolean offer(E e) { return add(e); } // Deque operations /** * 添加元素到首節點前面 */ public boolean offerFirst(E e) { addFirst(e); return true; } /** * 添加元素到尾節點後面 */ public boolean offerLast(E e) { addLast(e); return true; } /** * 獲取首節點不刪除 */ public E peekFirst() { final Node<E> f = first; return (f == null) ? null : f.item; } /** * 獲取尾節點不刪除 */ public E peekLast() { final Node<E> l = last; return (l == null) ? null : l.item; } /** * 獲取首節點並刪除 */ public E pollFirst() { final Node<E> f = first; return (f == null) ? null : unlinkFirst(f); } /** * 獲取尾節點並刪除 */ public E pollLast() { final Node<E> l = last; return (l == null) ? null : unlinkLast(l); } /** * 添加元素到首節點前面 */ public void push(E e) { addFirst(e); } /** * 移除並返回首節點 */ public E pop() { return removeFirst(); } // 節點構造 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; } } /** * 集合轉成數組 */ public Object[] toArray() { Object[] result = new Object[size]; int i = 0; for (Node<E> x = first; x != null; x = x.next) result[i++] = x.item; return result; } }View Code
LinkedList源碼淺析(jdk1.8)