LinkedList源码解析(JDK1.8)!

1.linkedlist简介

①. 其底层采用的双向链表结构。

② ArrayList 一样,LinkedList 也支持空值和重复值。由于 LinkedList 基于链表实现,存储元素过程中,无需像 ArrayList 那样进行扩容。但有得必有失,LinkedList 存储元素的节点需要额外的空间存储前驱和后继的引用。另一方面,LinkedList 在链表头部和尾部插入效率比较高,但在指定位置进行插入时,效率一般。原因是,在指定位置插入需要定位到该位置处的节点,此操作的时间复杂度为O(N)。

③ LinkedList 是非线程安全的集合类,并发环境下,多个线程同时操作 LinkedList,会引发不可预知的错误。
作者:田小波
链接:https://www.imooc.com/article/23151

2.linkedlist的继承实现(类,接口)图

public class LinkedList<E>
    extends AbstractSequentialList<E>
    implements List<E>, Deque<E>, Cloneable, java.io.Serializable

继承于AbstractSequentialList,后者继承于AbstractList;同时实现了四个接口:
List, Cloneable, java.io.Serializable,这三个接口同ArrayList一样的作用; Deque包括了双向链表的必须的一组方法;

如图:
《LinkedList源码解析(JDK1.8)!》
三个属性:

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

包括三个参数:元素值,指向下个元素指针,和指向前一个元素的指针。

构造器:
① 空构造器,不设置参数

public LinkedList() {
    }

②复制其他linkedlist来构造

 public LinkedList(Collection<? extends E> c) {
        this();
        addAll(c); 调用addAll方法
    }
 public boolean addAll(Collection<? extends E> c) {
        return addAll(size, c); 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++;  modeCount自增;
        return true;
    }

3.重要方法

①包括8个辅助方法:

linkFirst(E e):把目标元素e设置为头结点;

void linkLast(E e): e设置为尾结点;

void linkBefore(E e, Node succ): 在非空节点succ前插入元素e;

private E unlinkFirst(Node f): 删除头结点f;

private E unlinkLast(Node l) :删除尾结点l;

E unlink(Node x) :删除节点x;

public E getFirst(): 获取头结点
public E getLast() :尾结点

②contains()方法:

判断是否包含目标元素.

public boolean contains(Object o) {
        return indexOf(o) != -1;
    }
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;
    }

3.添加元素add()

调用linkLast方法,课件是新添加元素链到双向链表末尾。

public boolean add(E e) {
        linkLast(e);
        return true;
    }

4.删除remove()

删除目标元素;先判断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 E remove(int index) {
        checkElementIndex(index);
        return unlink(node(index));
    }

5.peek()

返回有节点的值;

public E peek() {
        final Node<E> f = first;
        return (f == null) ? null : f.item;
    }

包括peekFirst()

public E peekFirst() {
        final Node<E> f = first;
        return (f == null) ? null : f.item;
     }

peekLast(): 返回尾结点值;

public E peekLast() {
        final Node<E> l = last;
        return (l == null) ? null : l.item;
    }

6.poll()弹出节点:

public E poll() {
        final Node<E> f = first;
        return (f == null) ? null : unlinkFirst(f);
    }
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);
    }

7.remove()方法

包括remove,removeFirst() ,removeLast().

8.offer()方法

包括offer(),offerFirst(),offerLast()方法。

9.push()方法

public void push(E e) {
        addFirst(e);
    }

看到也是在头部添加。

10.其它方法

包括很多方法,但是大多不常用!像removeFirstOccurrence()和removeLastOccurrence()通过字面意思就能理解.
包括序列化readObject()和反序列化方法writeObject();

    原文作者:xuhuaabc
    原文地址: https://blog.csdn.net/xuhuaabc/article/details/92435749
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞