/** * 1、是否允许空:允许 * <p> * 2、是否允许重复数据:允许 * <p> * 3、是否有序,有序的意思是读取数据的顺序和存放数据的顺序是否一致:无序 * <p> * 4、是否线程安全:非线程安全 * <p> * 5、底层数据结构:双向链表。实现了Deque这个接口,所以也可以当栈和队列使用 */ package java.util; import java.util.function.Consumer; public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable, java.io.Serializable { /** * 元素个数 */ transient int size = 0; /** * 指向首节点 * Pointer to first node. * Invariant: (first == null && last == null) || * (first.prev == null && first.item != null) */ transient Node<E> first; /** * 指向尾结点 * Pointer to last node. * Invariant: (first == null && last == null) || * (last.next == null && last.item != null) */ transient Node<E> last; /** * 无参构造方法 * Constructs an empty list. */ public LinkedList() { } /** * 带集合参数的构造方法,元素顺序由集合的迭代器返回顺序决定 * Constructs a list containing the elements of the specified * collection, in the order they are returned by the collection's * iterator. * * @param c the collection whose elements are to be placed into this list * @throws NullPointerException if the specified collection is null */ public LinkedList(Collection<? extends E> c) { this(); addAll(c); } /** * 插入头结点 * Links e as first element. */ private void linkFirst(E e) { // 获取当前首节点引用 final Node<E> f = first; // new一个prev值为null,节点值为e,next值为f的新节点newNode final Node<E> newNode = new Node<>(null, e, f); // 把新节点作为首节点 first = newNode; // 如果原首节点为null,即原链表为空,把链表尾结点last设置为newNode if (f == null) last = newNode; else // 否则,把原首节点的前驱节点prev设置为newNode f.prev = newNode; // 元素个数+1 size++; // 修改次数+1 modCount++; } /** * 插入尾结点 * Links e as last element. */ void linkLast(E e) { // 获取当前尾结点引用 final Node<E> l = last; // new一个prev值为尾结点引用,节点值为e,next值为null的新节点newNode final Node<E> newNode = new Node<>(l, e, null); // 把新节点作为尾节点 last = newNode; // 如果原尾节点为null,即原链表为空,把链表首节点first设置为newNode if (l == null) first = newNode; else // 否则,把原尾节点的后继节点next设置为newNode l.next = newNode; // 元素个数+1 size++; // 修改次数+1 modCount++; } /** * 在非空节点succ前插入元素e * Inserts element e before non-null Node succ. */ void linkBefore(E e, Node<E> succ) { // 断言succ不为空assert succ != null; // 取出节点succ的前驱节点prev final Node<E> pred = succ.prev; // new一个prev值为pred,节点值为e,next值为succ的新节点newNode final Node<E> newNode = new Node<>(pred, e, succ); // 把新节点作为cucc的前驱节点prev succ.prev = newNode; // 如果pred为空,即succ为首节点,把新节点newNode设置为首节点 if (pred == null) first = newNode; else // 如果pred不为空,即succ不是首节点 pred.next = newNode; // 元素个数+1 size++; // 修改次数+1 modCount++; } /** * 删除首节点,返回其存储的元素(内部使用) * Unlinks non-null first node f. */ private E unlinkFirst(Node<E> f) { // 断言首节点不为空assert f == first && f != null; // 获取首节点存储的元素 final E element = f.item; // 获取首节点的后继节点next final Node<E> next = f.next; // 删除首节点 f.item = null; // 置空便于垃圾回收 f.next = null; // help GC // 把原首节点的后继节点next节点设置为首节点 first = next; // 如果原首节点的后继节点next为空,把尾结点last置空 if (next == null) last = null; else // 否则,把原首节点的后继节点next的前驱节点prev置为空 next.prev = null; // 元素个数-1 size--; // 修改次数+1 modCount++; // 返回原首节点存储的元素 return element; } /** * 删除尾结点,返回其存储的元素(内部使用) * Unlinks non-null last node l. */ 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 // 把原尾结点的前驱节点prev设置为尾结点 last = prev; // 如果原尾结点的前驱节点prev为空,把首节点置空 if (prev == null) first = null; else // 否则把原尾结点的前驱节点的后继节点next置空 prev.next = null; // 元素个数-1 size--; // 修改次数+1 modCount++; // 返回原尾结点元素 return element; } /** * 删除指定非空节点x,返回其存储的元素 * Unlinks non-null node x. */ E unlink(Node<E> x) { // 断言节点x不为空assert x != null; // 获取节点x存储的元素 final E element = x.item; // 获取节点x的后继节点next final Node<E> next = x.next; // 获取节点x的前驱节点prev final Node<E> prev = x.prev; // 如果prev为空,即节点x为首节点,把节点x的后继节点next设置为首节点 // 否则把节点x的后继节点设置为节点x的前驱节点的后继节点,节点x的前驱节点置空 if (prev == null) { first = next; } else { prev.next = next; x.prev = null; } // 如果next为空,即节点x为尾结点,把节点x的前驱节点设置为尾结点 // 否则把节点x的前驱节点设置节点x的后继节点的前驱节点 if (next == null) { last = prev; } else { next.prev = prev; x.next = null; } // 删除节点x x.item = null; // 元素个数-1 size--; // 修改次数+1 modCount++; // 返回节点x存储的元素 return element; } /** * 获取首节点存储的元素 * Returns the first element in this list. * * @return the first element in this list * @throws NoSuchElementException if this list is empty */ public E getFirst() { // 获取首节点引用 final Node<E> f = first; // 如果首节点引用为空,抛出异常 if (f == null) throw new NoSuchElementException(); // 否则返回首节点存储的元素 return f.item; } /** * 返回尾结点存储的元素 * Returns the last element in this list. * * @return the last element in this list * @throws NoSuchElementException if this list is empty */ public E getLast() { // 获取尾结点引用 final Node<E> l = last; // 如果尾结点为空,抛出异常 if (l == null) throw new NoSuchElementException(); // 否则返回尾结点存储的元素 return l.item; } /** * 删除并返回其存储的元素(外部使用) * Removes and returns the first element from this list. * * @return the first element from this list * @throws NoSuchElementException if this list is empty */ public E removeFirst() { // 获取首节点引用 final Node<E> f = first; // 如果首节点引用为空,抛出异常 if (f == null) throw new NoSuchElementException(); // 删除首节点并返回其存储的元素 return unlinkFirst(f); } /** * 删除尾结点并返回其存储的元素(外部使用) * Removes and returns the last element from this list. * * @return the last element from this list * @throws NoSuchElementException if this list is empty */ public E removeLast() { // 获取尾结点引用 final Node<E> l = last; // 如果尾结点为空,抛出异常 if (l == null) throw new NoSuchElementException(); // 删除尾结点并返回其存储的元素 return unlinkLast(l); } /** * 在头部插入指定元素e * Inserts the specified element at the beginning of this list. * * @param e the element to add */ public void addFirst(E e) { linkFirst(e); } /** * 在尾部插入指定元素e * Appends the specified element to the end of this list. * <p> * <p>This method is equivalent to {@link #add}. * * @param e the element to add */ public void addLast(E e) { linkLast(e); } /** * 判断是否包含指定元素o * <p> * Returns {@code true} if this list contains the specified element. * More formally, returns {@code true} if and only if this list contains * at least one element {@code e} such that * <tt>(o==null ? e==null : o.equals(e))</tt>. * * @param o element whose presence in this list is to be tested * @return {@code true} if this list contains the specified element */ public boolean contains(Object o) { // 获取指定元素的索引位置,不存在为-1,再与-1作比较 return indexOf(o) != -1; } /** * 返回元素个数 * Returns the number of elements in this list. * * @return the number of elements in this list */ public int size() { return size; } /** * 插入指定元素e,默认在尾部插入 * Appends the specified element to the end of this list. * <p> * <p>This method is equivalent to {@link #addLast}. * * @param e element to be appended to this list * @return {@code true} (as specified by {@link Collection#add}) */ public boolean add(E e) { linkLast(e); return true; } /** * 删除指定元素,默认从首节点开始,删除第一次出现的那个元素 * Removes the first occurrence of the specified element from this list, * if it is present. If this list does not contain the element, it is * unchanged. More formally, removes the element with the lowest index * {@code i} such that * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt> * (if such an element exists). Returns {@code true} if this list * contained the specified element (or equivalently, if this list * changed as a result of the call). * * @param o element to be removed from this list, if present * @return {@code true} if this list contained the specified element */ public boolean remove(Object o) { // 如果要删除的对象为null if (o == null) { // 遍历,查找到指定元素后删除该结点,返回true(用==作比较) for (Node<E> x = first; x != null; x = x.next) { if (x.item == null) { unlink(x); return true; } } } else { // 如果要删除的对象不为null // 遍历,查找到指定元素后删除该结点,返回true(用equals作比较) for (Node<E> x = first; x != null; x = x.next) { if (o.equals(x.item)) { unlink(x); return true; } } } return false; } /** * 将集合插入到链表尾部 * 开始位置索引为size * Appends all of the elements in the specified collection to the end of * this list, in the order that they are returned by the specified * collection's iterator. The behavior of this operation is undefined if * the specified collection is modified while the operation is in * progress. (Note that this will occur if the specified collection is * this list, and it's nonempty.) * * @param c collection containing elements to be added to this list * @return {@code true} if this list changed as a result of the call * @throws NullPointerException if the specified collection is null */ public boolean addAll(Collection<? extends E> c) { return addAll(size, c); } /** * Inserts all of the elements in the specified collection into this * list, starting at the specified position. Shifts the element * currently at that position (if any) and any subsequent elements to * the right (increases their indices). The new elements will appear * in the list in the order that they are returned by the * specified collection's iterator. * * @param index index at which to insert the first element * from the specified collection * @param c collection containing elements to be added to this list * @return {@code true} if this list changed as a result of the call * @throws IndexOutOfBoundsException {@inheritDoc} * @throws NullPointerException if the specified collection is null */ public boolean addAll(int index, Collection<? extends E> c) { // 检查索引是否越界 checkPositionIndex(index); // 转换成数组 Object[] a = c.toArray(); // 获取数组的长度 int numNew = a.length; // 长度等于0,即集合为空,直接返回false if (numNew == 0) return false; // 定义两个节点pred,succ,pred表示要插入元素的前驱节点,succ表示要插入元素的后继节点 Node<E> pred, succ; // 查询需要插入位置的前驱和后继节点 // 如果index == size,说明在尾部添加,所以succ = null,pred = last if (index == size) { succ = null; pred = last; } else { // 否则,succ为指定index位置的节点,pred为succ的前驱节点 succ = node(index); pred = succ.prev; } // 遍历数组并插入到链表中 for (Object o : a) { @SuppressWarnings("unchecked") E e = (E) o; // 创建一个prev为pred,元素为e,next为null的新节点newNode Node<E> newNode = new Node<>(pred, e, null); // 如果pred为空,则把新插入的节点作为链表首节点 if (pred == null) first = newNode; else // 否则把前驱节点的next指向新节点 pred.next = newNode; // pred指针向后移动,指向下一个待插入节点的前驱节点 pred = newNode; } // 如果后继节点为空,那么c集合中最后一个节点就是整个list的最后节点 if (succ == null) { last = pred; } else {// 否则,把c中的最后一个节点的后继节点指向succ,把后继节点的前驱节点指向c中的最后一个节点 pred.next = succ; succ.prev = pred; } // 元素个数为size+numNew size += numNew; // 修改次数+1 modCount++; return true; } /** * 删除整个链表 * Removes all of the elements from this list. * The list will be empty after this call returns. */ 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; // 修改次数+1 modCount++; } // Positional Access Operations /** * 获取指定位置的元素 * Returns the element at the specified position in this list. * * @param index index of the element to return * @return the element at the specified position in this list * @throws IndexOutOfBoundsException {@inheritDoc} */ public E get(int index) { checkElementIndex(index); return node(index).item; } /** * 修改指定位置的元素,返回修改前的元素 * Replaces the element at the specified position in this list with the * specified element. * * @param index index of the element to replace * @param element element to be stored at the specified position * @return the element previously at the specified position * @throws IndexOutOfBoundsException {@inheritDoc} */ public E set(int index, E element) { checkElementIndex(index); Node<E> x = node(index); E oldVal = x.item; x.item = element; return oldVal; } /** * 在指定位置前插入元素 * Inserts the specified element at the specified position in this list. * Shifts the element currently at that position (if any) and any * subsequent elements to the right (adds one to their indices). * * @param index index at which the specified element is to be inserted * @param element element to be inserted * @throws IndexOutOfBoundsException {@inheritDoc} */ public void add(int index, E element) { checkPositionIndex(index); if (index == size) linkLast(element); else linkBefore(element, node(index)); } /** * 删除指定位置的元素,并返回该元素 * Removes the element at the specified position in this list. Shifts any * subsequent elements to the left (subtracts one from their indices). * Returns the element that was removed from the list. * * @param index the index of the element to be removed * @return the element previously at the specified position * @throws IndexOutOfBoundsException {@inheritDoc} */ public E remove(int index) { checkElementIndex(index); return unlink(node(index)); } /** * Tells if the argument is the index of an existing element. */ private boolean isElementIndex(int index) { return index >= 0 && index < size; } /** * Tells if the argument is the index of a valid position for an * iterator or an add operation. */ private boolean isPositionIndex(int index) { return index >= 0 && index <= size; } /** * Constructs an IndexOutOfBoundsException detail message. * Of the many possible refactorings of the error handling code, * this "outlining" performs best with both server and client VMs. */ private String outOfBoundsMsg(int index) { return "Index: " + index + ", Size: " + size; } private void checkElementIndex(int index) { if (!isElementIndex(index)) throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); } /** * 检查越界 * * @param index */ private void checkPositionIndex(int index) { if (!isPositionIndex(index)) throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); } /** * 获取指定下标的结点 * Returns the (non-null) Node at the specified element 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 /** * 获取正向首次出现指定元素的位置 * 如果返回结果是-1,则表示不存在该元素 * <p> * Returns the index of the first occurrence of the specified element * in this list, or -1 if this list does not contain the element. * More formally, returns the lowest index {@code i} such that * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, * or -1 if there is no such index. * * @param o element to search for * @return the index of the first occurrence of the specified element in * this list, or -1 if this list does not contain the element */ 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; } /** * 获取逆向首次出现指定元素的位置 * 如果返回结果是-1,则表示不存在该元素 * <p> * Returns the index of the last occurrence of the specified element * in this list, or -1 if this list does not contain the element. * More formally, returns the highest index {@code i} such that * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, * or -1 if there is no such index. * * @param o element to search for * @return the index of the last occurrence of the specified element in * this list, or -1 if this list does not contain the element */ 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. /** * 出队,获取但不删除首元素 * Retrieves, but does not remove, the head (first element) of this list. * * @return the head of this list, or {@code null} if this list is empty * @since 1.5 */ public E peek() { final Node<E> f = first; return (f == null) ? null : f.item; } /** * 出队,获取但不删除首元素,若为null会抛出异常而不是返回null * Retrieves, but does not remove, the head (first element) of this list. * * @return the head of this list * @throws NoSuchElementException if this list is empty * @since 1.5 */ public E element() { return getFirst(); } /** * 获取并删除首元素 * Retrieves and removes the head (first element) of this list. * * @return the head of this list, or {@code null} if this list is empty * @since 1.5 */ public E poll() { final Node<E> f = first; return (f == null) ? null : unlinkFirst(f); } /** * 删除首节点元素并返回该元素,如果不存在会抛出异常而不是返回null * Retrieves and removes the head (first element) of this list. * * @return the head of this list * @throws NoSuchElementException if this list is empty * @since 1.5 */ public E remove() { return removeFirst(); } /** * 入队,在末尾添加指定元素 * Adds the specified element as the tail (last element) of this list. * * @param e the element to add * @return {@code true} (as specified by {@link Queue#offer}) * @since 1.5 */ public boolean offer(E e) { return add(e); } // 双端队列操作Deque operations /** * 入队,在头部插入指定元素 * Inserts the specified element at the front of this list. * * @param e the element to insert * @return {@code true} (as specified by {@link Deque#offerFirst}) * @since 1.6 */ public boolean offerFirst(E e) { addFirst(e); return true; } /** * 入队,在尾部插入指定元素 * Inserts the specified element at the end of this list. * * @param e the element to insert * @return {@code true} (as specified by {@link Deque#offerLast}) * @since 1.6 */ public boolean offerLast(E e) { addLast(e); return true; } /** * 出队,获取首元素,不存在会返回null,不会删除元素 * Retrieves, but does not remove, the first element of this list, * or returns {@code null} if this list is empty. * * @return the first element of this list, or {@code null} * if this list is empty * @since 1.6 */ public E peekFirst() { final Node<E> f = first; return (f == null) ? null : f.item; } /** * 出队,获得尾元素,不存在会返回null,不会删除元素 * Retrieves, but does not remove, the last element of this list, * or returns {@code null} if this list is empty. * * @return the last element of this list, or {@code null} * if this list is empty * @since 1.6 */ public E peekLast() { final Node<E> l = last; return (l == null) ? null : l.item; } /** * 出队,获取并删除首元素,不存在会返回null * Retrieves and removes the first element of this list, * or returns {@code null} if this list is empty. * * @return the first element of this list, or {@code null} if * this list is empty * @since 1.6 */ public E pollFirst() { final Node<E> f = first; return (f == null) ? null : unlinkFirst(f); } /** * 出队,获取并删除尾元素,不存在返回null * Retrieves and removes the last element of this list, * or returns {@code null} if this list is empty. * * @return the last element of this list, or {@code null} if * this list is empty * @since 1.6 */ public E pollLast() { final Node<E> l = last; return (l == null) ? null : unlinkLast(l); } /** * 入栈,在前面插入指定元素 * Pushes an element onto the stack represented by this list. In other * words, inserts the element at the front of this list. * <p> * <p>This method is equivalent to {@link #addFirst}. * * @param e the element to push * @since 1.6 */ public void push(E e) { addFirst(e); } /** * 出栈,返回栈顶元素并删除 * Pops an element from the stack represented by this list. In other * words, removes and returns the first element of this list. * <p> * <p>This method is equivalent to {@link #removeFirst()}. * * @return the element at the front of this list (which is the top * of the stack represented by this list) * @throws NoSuchElementException if this list is empty * @since 1.6 */ public E pop() { return removeFirst(); } /** * 删除正向首次出现的指定元素 * Removes the first occurrence of the specified element in this * list (when traversing the list from head to tail). If the list * does not contain the element, it is unchanged. * * @param o element to be removed from this list, if present * @return {@code true} if the list contained the specified element * @since 1.6 */ public boolean removeFirstOccurrence(Object o) { return remove(o); } /** * 删除逆向首次出现的指定元素 * Removes the last occurrence of the specified element in this * list (when traversing the list from head to tail). If the list * does not contain the element, it is unchanged. * * @param o element to be removed from this list, if present * @return {@code true} if the list contained the specified element * @since 1.6 */ public boolean removeLastOccurrence(Object o) { if (o == null) { for (Node<E> x = last; x != null; x = x.prev) { if (x.item == null) { unlink(x); return true; } } } else { for (Node<E> x = last; x != null; x = x.prev) { if (o.equals(x.item)) { unlink(x); return true; } } } return false; } /** * Returns a list-iterator of the elements in this list (in proper * sequence), starting at the specified position in the list. * Obeys the general contract of {@code List.listIterator(int)}.<p> * <p> * The list-iterator is <i>fail-fast</i>: if the list is structurally * modified at any time after the Iterator is created, in any way except * through the list-iterator's own {@code remove} or {@code add} * methods, the list-iterator will throw a * {@code ConcurrentModificationException}. Thus, in the face of * concurrent modification, the iterator fails quickly and cleanly, rather * than risking arbitrary, non-deterministic behavior at an undetermined * time in the future. * * @param index index of the first element to be returned from the * list-iterator (by a call to {@code next}) * @return a ListIterator of the elements in this list (in proper * sequence), starting at the specified position in the list * @throws IndexOutOfBoundsException {@inheritDoc} * @see List#listIterator(int) */ public ListIterator<E> listIterator(int index) { checkPositionIndex(index); return new ListItr(index); } private class ListItr implements ListIterator<E> { private Node<E> lastReturned; private Node<E> next; private int nextIndex; private int expectedModCount = modCount; ListItr(int index) { // assert isPositionIndex(index); next = (index == size) ? null : node(index); nextIndex = index; } public boolean hasNext() { return nextIndex < size; } public E next() { checkForComodification(); if (!hasNext()) throw new NoSuchElementException(); lastReturned = next; next = next.next; nextIndex++; return lastReturned.item; } public boolean hasPrevious() { return nextIndex > 0; } public E previous() { checkForComodification(); if (!hasPrevious()) throw new NoSuchElementException(); lastReturned = next = (next == null) ? last : next.prev; nextIndex--; return lastReturned.item; } public int nextIndex() { return nextIndex; } public int previousIndex() { return nextIndex - 1; } public void remove() { checkForComodification(); if (lastReturned == null) throw new IllegalStateException(); Node<E> lastNext = lastReturned.next; unlink(lastReturned); if (next == lastReturned) next = lastNext; else nextIndex--; lastReturned = null; expectedModCount++; } public void set(E e) { if (lastReturned == null) throw new IllegalStateException(); checkForComodification(); lastReturned.item = e; } public void add(E e) { checkForComodification(); lastReturned = null; if (next == null) linkLast(e); else linkBefore(e, next); nextIndex++; expectedModCount++; } public void forEachRemaining(Consumer<? super E> action) { Objects.requireNonNull(action); while (modCount == expectedModCount && nextIndex < size) { action.accept(next.item); lastReturned = next; next = next.next; nextIndex++; } checkForComodification(); } final void checkForComodification() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); } } /** * 节点的数据结构 * * @param <E> */ 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; } } /** * @since 1.6 */ public Iterator<E> descendingIterator() { return new DescendingIterator(); } /** * Adapter to provide descending iterators via ListItr.previous */ private class DescendingIterator implements Iterator<E> { private final ListItr itr = new ListItr(size()); public boolean hasNext() { return itr.hasPrevious(); } public E next() { return itr.previous(); } public void remove() { itr.remove(); } } @SuppressWarnings("unchecked") private LinkedList<E> superClone() { try { return (LinkedList<E>) super.clone(); } catch (CloneNotSupportedException e) { throw new InternalError(e); } } /** * 克隆,浅拷贝 * Returns a shallow copy of this {@code LinkedList}. (The elements * themselves are not cloned.) * * @return a shallow copy of this {@code LinkedList} instance */ public Object clone() { LinkedList<E> 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<E> x = first; x != null; x = x.next) clone.add(x.item); return clone; } /** * Returns an array containing all of the elements in this list * in proper sequence (from first to last element). * <p> * <p>The returned array will be "safe" in that no references to it are * maintained by this list. (In other words, this method must allocate * a new array). The caller is thus free to modify the returned array. * <p> * <p>This method acts as bridge between array-based and collection-based * APIs. * * @return an array containing all of the elements in this list * in proper sequence */ 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; } /** * Returns an array containing all of the elements in this list in * proper sequence (from first to last element); the runtime type of * the returned array is that of the specified array. If the list fits * in the specified array, it is returned therein. Otherwise, a new * array is allocated with the runtime type of the specified array and * the size of this list. * <p> * <p>If the list fits in the specified array with room to spare (i.e., * the array has more elements than the list), the element in the array * immediately following the end of the list is set to {@code null}. * (This is useful in determining the length of the list <i>only</i> if * the caller knows that the list does not contain any null elements.) * <p> * <p>Like the {@link #toArray()} method, this method acts as bridge between * array-based and collection-based APIs. Further, this method allows * precise control over the runtime type of the output array, and may, * under certain circumstances, be used to save allocation costs. * <p> * <p>Suppose {@code x} is a list known to contain only strings. * The following code can be used to dump the list into a newly * allocated array of {@code String}: * <p> * <pre> * String[] y = x.toArray(new String[0]);</pre> * <p> * Note that {@code toArray(new Object[0])} is identical in function to * {@code toArray()}. * * @param a the array into which the elements of the list are to * be stored, if it is big enough; otherwise, a new array of the * same runtime type is allocated for this purpose. * @return an array containing the elements of the list * @throws ArrayStoreException if the runtime type of the specified array * is not a supertype of the runtime type of every element in * this list * @throws NullPointerException if the specified array is null */ @SuppressWarnings("unchecked") 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; } private static final long serialVersionUID = 876323262645176354L; /** * 序列化 * Saves the state of this {@code LinkedList} instance to a stream * (that is, serializes it). * * @serialData The size of the list (the number of elements it * contains) is emitted (int), followed by all of its * elements (each an Object) in the proper order. */ private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException { // Write out any hidden serialization magic s.defaultWriteObject(); // Write out size s.writeInt(size); // Write out all elements in the proper order. for (Node<E> x = first; x != null; x = x.next) s.writeObject(x.item); } /** * 反序列化 * Reconstitutes this {@code LinkedList} instance from a stream * (that is, deserializes it). */ @SuppressWarnings("unchecked") private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { // Read in any hidden serialization magic s.defaultReadObject(); // Read in size int size = s.readInt(); // Read in all elements in the proper order. for (int i = 0; i < size; i++) linkLast((E) s.readObject()); } /** * Creates a <em><a href="Spliterator.html#binding">late-binding</a></em> * and <em>fail-fast</em> {@link Spliterator} over the elements in this * list. * <p> * <p>The {@code Spliterator} reports {@link Spliterator#SIZED} and * {@link Spliterator#ORDERED}. Overriding implementations should document * the reporting of additional characteristic values. * * @return a {@code Spliterator} over the elements in this list * @implNote The {@code Spliterator} additionally reports {@link Spliterator#SUBSIZED} * and implements {@code trySplit} to permit limited parallelism.. * @since 1.8 */ @Override public Spliterator<E> spliterator() { return new LLSpliterator<E>(this, -1, 0); } /** * A customized variant of Spliterators.IteratorSpliterator */ static final class LLSpliterator<E> implements Spliterator<E> { static final int BATCH_UNIT = 1 << 10; // batch array size increment static final int MAX_BATCH = 1 << 25; // max batch array size; final LinkedList<E> list; // null OK unless traversed Node<E> current; // current node; null until initialized int est; // size estimate; -1 until first needed int expectedModCount; // initialized when est set int batch; // batch size for splits LLSpliterator(LinkedList<E> list, int est, int expectedModCount) { this.list = list; this.est = est; this.expectedModCount = expectedModCount; } final int getEst() { int s; // force initialization final LinkedList<E> lst; if ((s = est) < 0) { if ((lst = list) == null) s = est = 0; else { expectedModCount = lst.modCount; current = lst.first; s = est = lst.size; } } return s; } public long estimateSize() { return (long) getEst(); } public Spliterator<E> trySplit() { Node<E> p; int s = getEst(); if (s > 1 && (p = current) != null) { int n = batch + BATCH_UNIT; if (n > s) n = s; if (n > MAX_BATCH) n = MAX_BATCH; Object[] a = new Object[n]; int j = 0; do { a[j++] = p.item; } while ((p = p.next) != null && j < n); current = p; batch = j; est = s - j; return Spliterators.spliterator(a, 0, j, Spliterator.ORDERED); } return null; } public void forEachRemaining(Consumer<? super E> action) { Node<E> p; int n; if (action == null) throw new NullPointerException(); if ((n = getEst()) > 0 && (p = current) != null) { current = null; est = 0; do { E e = p.item; p = p.next; action.accept(e); } while (p != null && --n > 0); } if (list.modCount != expectedModCount) throw new ConcurrentModificationException(); } public boolean tryAdvance(Consumer<? super E> action) { Node<E> p; if (action == null) throw new NullPointerException(); if (getEst() > 0 && (p = current) != null) { --est; E e = p.item; current = p.next; action.accept(e); if (list.modCount != expectedModCount) throw new ConcurrentModificationException(); return true; } return false; } public int characteristics() { return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED; } } }
来源:oschina
链接:https://my.oschina.net/naisitumitiu/blog/4285501