JDK源码util包分析——LinkedList源码(5)

本文详细分析了LinkedList的内部结构和实现,包括它的双向链表特性、作为List和Deque接口的实现,以及相关的方法如add、remove、get等。此外,还介绍了LinkedList的迭代器实现,包括正序和降序迭代。
摘要由CSDN通过智能技术生成

LinkedList源码分析

LinkedList的结构图

在这里插入图片描述

LinkedList的介绍

LinkedList 是一个继承于AbstractSequentialList的双向链表。它也可以被当作堆栈、队列或双端队列进行操作。
LinkedList 实现 List 接口,能对它进行队列操作。
LinkedList 实现 Deque 接口,即能将LinkedList当作双端队列使用。
LinkedList 实现了Cloneable接口,即覆盖了函数clone(),能克隆。
LinkedList 实现java.io.Serializable接口,这意味着LinkedList支持序列化,能通过序列化去传输。
LinkedList 是非同步的。
LinkedList底层的数据结构是基于双向循环链表的,且头结点中不存放数据。
在这里插入图片描述

	//当前链表中一共有多少节点
	transient int size = 0;

    //指向链表的第一个节点
    transient Node<E> first;

    //指向链表的最后一个节点
    transient Node<E> last;
	//节点Node定义
     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;
        }
    }

ArrayList的构造方法

    public LinkedList() {
    }
    
    public LinkedList(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);
        // 将给定的Collection对象转为Object数组
        Object[] a = c.toArray();
        int numNew = a.length;
        // 数组为空的话,直接返回false
        if (numNew == 0)
            return false;
        // 数组不为空
        Node<E> pred, succ;
        if (index == size) {
        	// 构造方法调用的时候,index = size = 0,进入这个条件。
            succ = null;
            pred = last;
        } else {
        	// 链表非空时调用,node方法返回给定索引位置的节点对象
            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; // 将当前链表最后一个节点赋值给last
        } else {
        	// 链表非空时,将断开的部分连接上
            pred.next = succ;
            succ.prev = pred;
        }
        // 记录当前节点个数
        size += numNew;
        modCount++;
        return true;
    }

LinkedList的简单方法

//设置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指向e(既最后一个节点)
            last = newNode;
        else//链表中存在节点,将该节点的pre指向e(既连接e与其他节点)
            f.prev = newNode;
        size++;
        modCount++;
    }
//设置e为尾节点
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++;
    }
//将e元素添加到succ节点前
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;//令头节点值为null
        f.next = null; // help GC//令头节点指向下一节点指针为null
        first = next;
        if (next == null)//如果下一节点为null,说明该链表无节点
            last = 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;//得到x节点的值
        final Node<E> next = x.next;//得到x节点的后节点
        final Node<E> prev = x.prev;//得到x节点的前节点

        if (prev == null) {//如果前节点为null,则说明x节点为头节点
            first = next;
        } else {
            prev.next = next;
            x.prev = null;
        }

        if (next == null) {//如果后节点为null,则说明x节点为尾节点
            last = prev;
        } else {
            next.prev = prev;
            x.next = null;
        }

        x.item = null;
        size--;
        modCount++;
        return element;
    }
//————————————————————————————————Deque————————————————————————————————————————————
//得到头节点的值
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();
        //调用unlinkFirst方法(在上方)
        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) {
		//调用indexOf方法(在下方)
        return indexOf(o) != -1;
    }
//返回o第一次出现的索引
public int indexOf(Object o) {
        int index = 0;
        if (o == null) {//如果o为null值
            for (Node<E> x = first; x != null; x = x.next) {
                if (x.item == null)//x的值为null值
                    return index;
                index++;
            }
        } else {//如果o不为null值
            for (Node<E> x = first; x != null; x = x.next) {
                if (o.equals(x.item))
                    return index;
                index++;
            }
        }
        return -1;
    }
//返回o最后一次出现的索引
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;
    }
//————————————————————————————————Deque————————————————————————————————————————————
//返回链表中节点数目
public int size() {
        return size;
    }
//添加e元素到链表最后
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 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++;
    }
//得到index处的节点的值
public E get(int index) {
        checkElementIndex(index);//检查index是否越界
        return node(index).item;
    }
//将index处节点的值设置为element,并返回旧值
public E set(int index, E element) {
        checkElementIndex(index);
        Node<E> x = node(index);//调用node方法(在下方)
        E oldVal = x.item;//得到该节点的值
        x.item = element;//设置该节点的为element
        return oldVal;
    }
//找到指定index处的节点
Node<E> node(int index) {
        // assert isElementIndex(index);
		
        if (index < (size >> 1)) {//size右移1位(既size除二),判断index是否小于size/2,如果小于,则从前向后遍历寻找
            Node<E> x = first;
            for (int i = 0; i < index; i++)
                x = x.next;
            return x;
        } else {//如果index大于size/2,则从后向前遍历寻找
            Node<E> x = last;
            for (int i = size - 1; i > index; i--)
                x = x.prev;
            return x;
        }
    }
//在index处插入元素element
public void add(int index, E element) {
        checkPositionIndex(index);//检查index是否越界

        if (index == size)//如果index与链表长度相同,则直接添加在最后即可
            linkLast(element);
        else
            linkBefore(element, node(index));//node()方法得到index处的节点,linkBefore()方法将element元素添加在该节点之前
    }
//删除index处的节点
public E remove(int index) {
        checkElementIndex(index);
        return unlink(node(index));
    }
//————————————————————————————————Deque下方—————————————————————————————————————————
//得到头节点值,但不删除
public E peek() {
        final Node<E> f = first;
        return (f == null) ? null : f.item;
    }
//得到头节点值,但不删除
public E element() {
        return getFirst();//getFirst方法:如果头节点为null会抛出异常
    }
//删除头节点
public E poll() {
        final Node<E> f = first;
        return (f == null) ? null : unlinkFirst(f);
    }
//删除头节点
public E remove() {
        return removeFirst();//removeFirst方法:如果头节点为null会抛出异常
    }
/*
1.作为List使用时,一般采用add / get方法来 压入/获取对象。
2.作为Queue使用时专,才会采用 offer/poll/take等方属法。
3.作为链表对象时,offer等方法相对来说,没有什么意义,这些方法是用于支持队列应用的。
*/
//添加e元素到链表最后
public boolean offer(E e) {
        return add(e);
    }
//添加e元素为头节点
public boolean offerFirst(E e) {
        addFirst(e);
        return true;
    }
//添加e元素为尾节点
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();
    }
//删除第一个出现的o元素
public boolean removeFirstOccurrence(Object o) {
        return remove(o);
    }
//删除最后一个出现的o元素
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;
    }


//————————————————————————————————Deque上方—————————————————————————————————————————

LinkedList的重要方法

//调用父类的clone进行复制
private LinkedList<E> superClone() {
        try {
            return (LinkedList<E>) super.clone();
        } catch (CloneNotSupportedException e) {
            throw new InternalError(e);
        }
    }
//克隆(克隆的集合与原始集合没有内在联系)
public Object clone() {
		//调用superClone方法(上方)
        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;
    }
//返回该集合的值的数组
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;
    }
//方法返回一个包含所有在此列表中正确的序列中的元素(从第一个到最后一个元素)数组;返回数组的运行时类型是指定数组的。如果列表中指定的数组能容纳,则在其中返回。否则,一个新的数组分配具有指定数组的运行时类型和此列表的大小。
public <T> T[] toArray(T[] a) {
        if (a.length < size)//判断指定列表数组的大小是否小于该列表的大小
        //如果小于,则重新创建一个新的以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 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);
    }
//读取流中的集合(反序列化)
 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());
    }

LinkList的迭代器

双向迭代器

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();
        }
    }

降序迭代器

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();
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值