LinkedList源码学习笔记

LinkedList简介

   博文参考https://blog.csdn.net/ns_code/article/details/35787253

    LinkedList是基于双向循环链表(从源码中可以很容易看出)实现的,除了可以当做链表来操作外,它还可以当做栈、队列和双端队列来使用。

    LinkedList同样是非线程安全的,只在单线程下适合使用。

    LinkedList实现了Serializable接口,因此它支持序列化,能够通过序列化传输,实现了Cloneable接口,能被克隆。

    在看本文源码解析之前建议没有看过数据结构的读者先看看数据结构中的双向链表是怎么插入和删除元素的。

    这里放两个双向链表插入和删除的操作图:

   插入:

删除:

LinkedList源码剖析

modCount简介:

扫描二维码关注公众号,回复: 3141296 查看本文章

由以上代码可以看出,在一个迭代器初始的时候会赋予它调用这个迭代器的对象的mCount,如何在迭代器遍历的过程中,一旦发现这个对象的mcount和迭代器中存储的mcount不一样那就抛异常 
好的,下面是这个的完整解释 
Fail-Fast 机制 
我们知道 java.util.HashMap 不是线程安全的,因此如果在使用迭代器的过程中有其他线程修改了map,那么将抛出ConcurrentModificationException,这就是所谓fail-fast策略。这一策略在源码中的实现是通过 modCount 域,modCount 顾名思义就是修改次数,对HashMap 内容的修改都将增加这个值,那么在迭代器初始化过程中会将这个值赋给迭代器的 expectedModCount。在迭代过程中,判断 modCount 跟 expectedModCount 是否相等,如果不相等就表示已经有其他线程修改了 Map:注意到 modCount 声明为 volatile,保证线程之间修改的可见性。

所以在这里和大家建议,当大家遍历那些非线程安全的数据结构时,尽量使用迭代器

transient关键字

     1,一旦变量被transient修饰,变量将不再是对象持久化的一部分,该变量内容在序列化后无法获得访问。 
  2,transient关键字只能修饰变量,而不能修饰方法和类。注意,本地变量是不能被transient关键字修饰的。变量如果是用户自定义类变量,则该类需要实现Serializable接口。 
  3,被transient关键字修饰的变量不再能被序列化,一个静态变量不管是否被transient修饰,均不能被序列化。 
  对于第三点,加上static之后,依然能把姓名输出。这是因为:反序列化后类中static型变量name的值为当前JVM中对应static变量的值,这个值是JVM中的不是反序列化得出的。下例可说明,其值时JVM中得到的而不是反序列化得到的:

public class LinkedList<E>  
    extends AbstractSequentialList<E>  
    implements List<E>, Deque<E>, Cloneable, java.io.Serializable  
{  
    // 链表的表头,表头不包含任何数据。Entry是个链表类数据结构。  
    private transient Entry<E> header = new Entry<E>(null, null, null);  
 
    // LinkedList中元素个数  
    private transient int size = 0;  
 
    // 默认构造函数:创建一个空的链表  
    public LinkedList() {  
        header.next = header.previous = header;  
    }  
 
    // 包含“集合”的构造函数:创建一个包含“集合”的LinkedList  
    public LinkedList(Collection<? extends E> c) {  
        this();  
        addAll(c);  
    }  

    // 双向链表的节点所对应的数据结构。  
    // 包含3部分:上一节点,下一节点,当前节点值。  
    private static class Entry<E> {  
        // 当前节点所包含的值  
        E element;  
        // 下一个节点  
        Entry<E> next;  
        // 上一个节点  
        Entry<E> previous;  
 
        /**  
         * 链表节点的构造函数。  
         * 参数说明:  
         *   element  —— 节点所包含的数据  
         *   next      —— 下一个节点  
         *   previous —— 上一个节点  
         */ 
        Entry(E element, Entry<E> next, Entry<E> previous) {  
            this.element = element;  
            this.next = next;  
            this.previous = previous;  
        }  
    }  


 // 将节点(节点数据是e)添加到entry节点之前。  
    private Entry<E> addBefore(E e, Entry<E> entry) {  
        // 新建节点newEntry,将newEntry插入到节点e之前;并且设置newEntry的数据是e  
        Entry<E> newEntry = new Entry<E>(e, entry, entry.previous);  
        newEntry.previous.next = newEntry;  
        newEntry.next.previous = newEntry;  
        // 修改LinkedList大小  
        size++;  
        // 修改LinkedList的修改统计数:用来实现fail-fast机制。  
        modCount++;  
        return newEntry;  
    }  

 // 将元素(E)添加到LinkedList中  
    public boolean add(E e) {  
        // 将节点(节点数据是e)添加到表头(header)之前。  
        // 即,将节点添加到双向链表的末端。  
        addBefore(e, header);  
        return true;  
    }  

   // 将元素添加到LinkedList的起始位置  
    public void addFirst(E e) {  
        addBefore(e, header.next);  
    }  
 
    // 将元素添加到LinkedList的结束位置  
    public void addLast(E e) {  
        addBefore(e, header);  
    }  

  // 在index前添加节点,且节点的值为element  
    public void add(int index, E element) {  
        addBefore(element, (index==size ? header : entry(index)));  
    }  

   // 设置index位置对应的节点的值为element  
    public E set(int index, E element) {  
        Entry<E> e = entry(index);  
        E oldVal = e.element;  
        e.element = element;  
        return oldVal;  
    }  

  // 返回LinkedList指定位置的元素  
    public E get(int index) {  
        return entry(index).element;  
    }  

 // 获取LinkedList的第一个元素  
    public E getFirst() {  
        if (size==0)  
            throw new NoSuchElementException();  
 
        // 链表的表头header中不包含数据。  
        // 这里返回header所指下一个节点所包含的数据。  
        return header.next.element;  
    }  
 
    // 获取LinkedList的最后一个元素  
    public E getLast()  {  
        if (size==0)  
            throw new NoSuchElementException();  
 
        // 由于LinkedList是双向链表;而表头header不包含数据。  
        // 因而,这里返回表头header的前一个节点所包含的数据。  
        return header.previous.element;  
    }  



  // 将节点从链表中删除  
    private E remove(Entry<E> e) {  
        if (e == header)  
            throw new NoSuchElementException();  
 
        E result = e.element;  
        e.previous.next = e.next;  
        e.next.previous = e.previous;  
        e.next = e.previous = null;  
        e.element = null;  
        size--;  
        modCount++;  
        return result;  
    }  

  // 删除LinkedList的第一个元素  
    public E removeFirst() {  
        return remove(header.next);  
    }  
 
    // 删除LinkedList的最后一个元素  
    public E removeLast() {  
        return remove(header.previous);  
    }  

 // 删除并返回第一个节点  
    // 若LinkedList的大小为0,则返回null  
    public E poll() {  
        if (size==0)  
            return null;  
        return removeFirst();  
    }  

 // 返回第一个节点  
    // 若LinkedList的大小为0,则返回null  
    public E peekFirst() {  
        if (size==0)  
            return null;  
        return getFirst();  
    }  
 
    // 返回最后一个节点  
    // 若LinkedList的大小为0,则返回null  
    public E peekLast() {  
        if (size==0)  
            return null;  
        return getLast();  
    }  
 
    // 删除并返回第一个节点  
    // 若LinkedList的大小为0,则返回null  
    public E pollFirst() {  
        if (size==0)  
            return null;  
        return removeFirst();  
    }  
 
    // 删除并返回最后一个节点  
    // 若LinkedList的大小为0,则返回null  
    public E pollLast() {  
        if (size==0)  
            return null;  
        return removeLast();  
    }  
 
    // 将e插入到双向链表开头  
    public void push(E e) {  
        addFirst(e);  
    }  
 
    // 删除并返回第一个节点  
    public E pop() {  
        return removeFirst();  
    }  



// List迭代器  
    private class ListItr implements ListIterator<E> {  
        // 上一次返回的节点  
        private Entry<E> lastReturned = header;  
        // 下一个节点  
        private Entry<E> next;  
        // 下一个节点对应的索引值  
        private int nextIndex;  
        // 期望的改变计数。用来实现fail-fast机制。  
        private int expectedModCount = modCount;  
 
        // 构造函数。  
        // 从index位置开始进行迭代  
        ListItr(int index) {  
            // index的有效性处理  
            if (index < 0 || index > size)  
                throw new IndexOutOfBoundsException("Index: "+index+ ", Size: "+size);  
            // 若 “index 小于 ‘双向链表长度的一半’”,则从第一个元素开始往后查找;  
            // 否则,从最后一个元素往前查找。  
            if (index < (size >> 1)) {  
                next = header.next;  
                for (nextIndex=0; nextIndex<index; nextIndex++)  
                    next = next.next;  
            } else {  
                next = header;  
                for (nextIndex=size; nextIndex>index; nextIndex--)  
                    next = next.previous;  
            }  
        }  
 
        // 是否存在下一个元素  
        public boolean hasNext() {  
            // 通过元素索引是否等于“双向链表大小”来判断是否达到最后。  
            return nextIndex != size;  
        }  
 
        // 获取下一个元素  
        public E next() {  
            checkForComodification();  
            if (nextIndex == size)  
                throw new NoSuchElementException();  
 
            lastReturned = next;  
            // next指向链表的下一个元素  
            next = next.next;  
            nextIndex++;  
            return lastReturned.element;  
        }  
 
        // 是否存在上一个元素  
        public boolean hasPrevious() {  
            // 通过元素索引是否等于0,来判断是否达到开头。  
            return nextIndex != 0;  
        }  
 
        // 获取上一个元素  
        public E previous() {  
            if (nextIndex == 0)  
            throw new NoSuchElementException();  
 
            // next指向链表的上一个元素  
            lastReturned = next = next.previous;  
            nextIndex--;  
            checkForComodification();  
            return lastReturned.element;  
        }  
 
        // 获取下一个元素的索引  
        public int nextIndex() {  
            return nextIndex;  
        }  
 
        // 获取上一个元素的索引  
        public int previousIndex() {  
            return nextIndex-1;  
        }  
 
        // 删除当前元素。  
        // 删除双向链表中的当前节点  
        public void remove() {  
            checkForComodification();  
            Entry<E> lastNext = lastReturned.next;  
            try {  
                LinkedList.this.remove(lastReturned);  
            } catch (NoSuchElementException e) {  
                throw new IllegalStateException();  
            }  
            if (next==lastReturned)  
                next = lastNext;  
            else 
                nextIndex--;  
            lastReturned = header;  
            expectedModCount++;  
        }  
 
        // 设置当前节点为e  
        public void set(E e) {  
            if (lastReturned == header)  
                throw new IllegalStateException();  
            checkForComodification();  
            lastReturned.element = e;  
        }  
 
        // 将e添加到当前节点的前面  
        public void add(E e) {  
            checkForComodification();  
            lastReturned = header;  
            addBefore(e, next);  
            nextIndex++;  
            expectedModCount++;  
        }  
 
        // 判断 “modCount和expectedModCount是否相等”,依次来实现fail-fast机制。  
        final void checkForComodification() {  
            if (modCount != expectedModCount)  
            throw new ConcurrentModificationException();  
        }  
    }  


猜你喜欢

转载自blog.csdn.net/wind_cp/article/details/82562451