Java class set framework-wide list source code analysis
Shortlist
The linked list is implemented based on a two-way cyclic linked list. It can also be used as a stack, queue, or double-end queue for operations. Non-thread security. The following describes the Java Implementation of ArrayList (only part of the code is pasted). The source is JDK1.8.0 _ 25/src.zip.
/******** The data structure of the two-way linked list ********** includes: the node value item * pre * next * @ param
*/Private static class Node
{E item; Node
Next; Node
Prev; // The parameter sequence of the constructor: the value of the precursor Node (Node
Prev, E element, Node
Next) {this. item = element; this. next = next; this. prev = prev ;}}
Public class upload list
Extends AbstractSequentialList
Implements List
, Deque
, Cloneable, java. io. Serializable {// number of elements transient int size = 0; // point to the first Node transient Node
First; // point to the last Node transient Node
Last; // constructor 1 -- construct an empty list public collections list () {}// constructor 2 -- construct a list containing elements in the specified collection, sort the public Collections list (collection
C) {this (); addAll (c) ;}// insert e to the front of the first position of the linked list. Note that last and first private void linkFirst (E) may be modified) {final Node
F = first; final Node
NewNode = new Node <> (null, e, f); // Node (precursor, element, successor) first = newNode; if (f = null) last = newNode; // if it is null before insertion, the last value is its own else f. prev = newNode; // if it is not empty, the header node is directed to newnode size ++; modCount ++ ;} // insert e to the end of the last element of the linked list. void linkLast (E) {final Node
L = last; final Node
NewNode = new Node <> (l, e, null); last = newNode; if (l = null) first = newNode; else l. next = newNode; size ++; modCount ++;} // Insert the void linkBefore (e E, Node
Succ) {// assert succ! = Null; final Node
Pred = succ. prev; final Node
NewNode = new Node <> (pred, e, succ); succ. prev = newNode; if (pred = null) first = newNode; else pred. next = newNode; size ++; modCount ++;} // remove the first element of the linked list private E unlinkFirst (Node
F) {// assert f = first & f! = Null; final E element = f. item; final Node
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;} // remove the last element of the linked list private E unlinkLast (Node
L) {// assert l = last & l! = Null; final E element = l. item; final Node
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;} // delete a Node x E unlink (Node
X) {// assert x! = Null; final E element = x. item; final Node
Next = x. next; final Node
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;} // gets the first element. If it is null, an exception public E getFirst () {final Node is thrown.
F = first; if (f = null) throw new NoSuchElementException (); return f. item;} // obtain the first element. If it is null, an exception public E getLast () {final Node is thrown.
L = last; if (l = null) throw new NoSuchElementException (); return l. item;} // remove the first element public E removeFirst () {final Node
F = first; if (f = null) throw new NoSuchElementException (); return unlinkFirst (f) ;}// remove the last element public E removeLast () {final Node
L = last; if (l = null) throw new NoSuchElementException (); return unlinkLast (l );} // Add Element e as the first element of the linked list public void addFirst (E) {linkFirst (e);} public void addLast (e E) {linkLast (e );} public boolean contains (Object o) {return indexOf (o )! =-1 ;}public int size () {return size;} public boolean add (E e) {linkLast (e); // add it to the end and return true ;} // The null element can be removed and the specified o public boolean remove (Object o) {if (o = null) {for (Node
X = first; x! = Null; x = x. next) {if (x. item = null) {unlink (x); return true ;}} else {for (Node
X = first; x! = Null; x = x. next) {if (o. equals (x. item) {unlink (x); return true ;}} return false ;}// add set c to public boolean addAll (Collection
C) {return addAll (size, c);} // Add the elements in set c to the public boolean addAll (int index, Collection
C) {checkPositionIndex (index); Object [] a = c. toArray (); int numNew =. length; if (numNew = 0) // set c to null, return false; Node
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
NewNode = new Node <> (pred, e, null); if (pred = null) first = newNode; // always change first and last else pred. next = newNode; pred = newNode;} if (succ = null) {last = pred;} else {pred. next = succ; succ. prev = pred;} size + = numNew; // after the list is added, modCount ++; return true;} // clear the public void clear () {for (Node
X = first; x! = Null;) {Node
Next = x. next; x. item = null; x. next = null; x. prev = null; x = next;} first = last = null; size = 0; modCount ++ ;} // Positional Access Operations // obtain the specified position element public E get (int index) {checkElementIndex (index); return node (index ). item;} // set the value of the specified position element, and return the original value public E set (int index, E element) {checkElementIndex (index); Node
X = node (index); E oldVal = x. item; x. item = element; return oldVal;} // ----- add the node whose element value is element before index ---- public void add (int index, E element) {checkPositionIndex (index ); if (index = size) linkLast (element); else linkBefore (element, node (index);} // Delete the public E remove (int index) node at the specified position) {checkElementIndex (index); return unlink (node (index);} private boolean isElementIndex (int index) {return in Dex> = 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);} // if the previous version is entry, obtain the Node at the specified position
Node (int index) {// assert isElementIndex (index); // if the position to be searched is less than half of the two-way linked list, search from the beginning to the end. Otherwise, search from the back to the end, improve search efficiency if (index <(size> 1) {Node
X = first; for (int I = 0; I <index; I ++) x = x. next; return x;} else {Node
X = last; for (int I = size-1; I> index; I --) x = x. prev; return x ;}// Search Operations // locate the node where the return value is o. If no result exists,-1 is returned, the null location can be searched. // The public int indexOf (Object o) {int index = 0; if (o = null) {for (Node
X = first; x! = Null; x = x. next) {if (x. item = null) return index; index ++ ;}} else {for (Node
X = first; x! = Null; x = x. next) {if (o. equals (x. item) return index; index ++;} return-1;} // search from the back and forth. The return value is the node location of o. If no result exists,-1 is returned, the null location can be searched. // The public int lastIndexOf (Object o) {int index = size; if (o = null) {for (Node
X = last; x! = Null; x = x. prev) {index --; if (x. item = null) return index ;}} else {for (Node
X = last; x! = Null; x = x. prev) {index --; if (o. equals (x. item) return index ;}} return-1;}/******* as the Queue operation Queue operations. * *********** // returns the first node element. If it is null, null is returned, and public E peek () is obtained but not removed () {final Node
F = first; return (f = null )? Null: f. item;} // returns the first node element. If it is null, an exception is thrown, which is equivalent to the public E element () {return getFirst, get and remove the header public E poll () {final Node
F = first; return (f = null )? Null: unlinkFirst (f);} public E remove () {return removeFirst ();} // Add to the end of the table 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
F = first; return (f = null )? Null: f. item;} public E peekLast () {final Node
L = last; return (l = null )? Null: l. item;} public E pollFirst () {final Node
F = first; return (f = null )? Null: unlinkFirst (f);} public E pollLast () {final Node
L = last; return (l = null )? Null: unlinkLast (l);} public void push (E) {addFirst (e);} public e pop () {return removeFirst ();} public boolean removeFirstOccurrence (Object o) {return remove (o);} // removes the last occurrence of a specified node, you can remove the first Node from the last query to make it null public boolean removeLastOccurrence (Object o) {if (o = null) {for (Node
X = last; x! = Null; x = x. prev) {if (x. item = null) {unlink (x); return true ;}} else {for (Node
X = last; x! = Null; x = x. prev) {if (o. equals (x. item) {unlink (x); return true ;}} return false ;}// return the ListIterator object public ListIterator corresponding to all nodes at the end of the index.
ListIterator (int index) {checkPositionIndex (index); return new ListItr (index);} // List iterator private class ListItr implements ListIterator
{Private Node
LastReturned; // private Node of the last returned Node
Next; // The next node private int nextIndex; // the index of the next node // expected change count, used to implement the fail-fast mechanism private int expectedModCount = modCount; ListItr (int index) {// assert isPositionIndex (index); next = (index = size )? Null: node (index); nextIndex = index ;}
public Object clone() { LinkedList
clone = superClone(); // Put clone into "virgin" state clone.first = clone.last = null; clone.size = 0; clone.modCount = 0; // Initialize clone with our elements for (Node
x = first; x != null; x = x.next) clone.add(x.item); return clone; }
Constructor:
There are two methods to construct the shortlist: one is the construction method without parameters, and the other is to create an empty linked list (the header node is null), and the other is to create an empty linked list first, and then call addAll (c) use the elements in c for initialization.
Note:
The shortlist is implemented based on a two-way cyclic linked list. There is no insufficient capacity or resizing, but the insertion and deletion efficiency is high. When you insert a node, create a new node object, locate the location of the inserted node, and change the frontend and successor. When you delete a node, you do not need to move any element when deleting the node's precursor node.
The partition list allows null elements. When searching for and deleting an element, both null and non-null elements are distinguished.
The pull List implements stack and queue operations, such as push () pop () and other methods. to distinguish different operations, many methods have different names, but their functions are the same.