LinkedList源码解析

版本:jdk1.8

底层由双向链表实现
线程不安全
适合大量随机删除、插入
不合适频繁的查询
删除和插入 只是指针的移动 与Arraylist(扩容以及数组的移动)相比是用空间去换取时间
查询LinkedList会把index与(size/2)作比较 选取是从头还是从尾开始查询O(N/2) Arraylist是随机访问时间复杂度O(1)

底层数据数据结构:双向链表
在这里插入图片描述

LinkedList相关的UML

在这里插入图片描述
重要的属性

	/**
	 * 	集合大小
	 */
	transient int size = 0;
	
	/**
	 * 	头节点指针
	 */
    transient Node<E> first;

    /**
     *	尾结点指针
     */
    transient Node<E> last;

链表节点的数据结构

    /**
     * LinkedList最重要的数据结构 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() {
    }

    /**
     * 把Collection中的元素加入LinkedList中
     * @param c 集合
     */
    public LinkedList(Collection<? extends E> c) {
        this();//先调用无参构造
        addAll(c);
    }

先来看看作为List它的几个实现方法
listStr.add
从链表的尾部添加数据

    /**
     * 	插入尾结点
     */
    public boolean add(E e) {
        linkLast(e);
        return true;
    }
    /**
     * 	把e作为尾结点
     */
    void linkLast(E e) {
        final Node<E> l = last;//把l指向尾节点
        final Node<E> newNode = new Node<>(l, e, null);//初始化一个新的节点
        last = newNode;//last指向newNode
        if (l == null)//说明last还没初始化 这时候 first==last==null
            first = newNode;//把first也指向newNode
        else
            l.next = newNode;//之前尾节点的next节点指向newNode
        size++;//集合大小加一
        modCount++;
    }

listStr.get
这里就可以看出LinkedList查询是二分之一个遍历链表获取到数据的

    /**
     * 	获取index处的节点数据
     */
    public E get(int index) {
        checkElementIndex(index);//校验下标
        return node(index).item;
    }
	    /**
     * 	获取index位置的Node 为降低时间复杂度  用了二分查找
     */
    Node<E> node(int index) {
        if (index < (size >> 1)) {//下标小于size的一半 就从头节点向后开始遍历
            Node<E> x = first;
            for (int i = 0; i < index; i++)
                x = x.next;
            return x;
        } else {//下标大于等于size的一半 就从尾结点向前开始遍历
            Node<E> x = last;
            for (int i = size - 1; i > index; i--)
                x = x.prev;
            return x;
        }
    }

listStr.set
找到index位置的node,覆盖旧值

    /**
     * 	在下标index处设置数据e 返回就值
     */
    public E set(int index, E element) {
        checkElementIndex(index);//校验下标
        Node<E> x = node(index);//获取node
        E oldVal = x.item;// 获取旧值
        x.item = element;// 用新值覆盖旧值
        return oldVal;
    }

listStr.remove
找到index处的节点,然后删除

    /**
     * 	移除index处的节点
     */
    public E remove(int index) {
        checkElementIndex(index);//校验下标
        return unlink(node(index));// 删除当前节点 x
    }

使用LinkedList可以实现队列(先进先出)、栈(先进后出),因为LinkedList也是一个Deque双端队列
队列:头进尾出、尾进头出
栈 :头进头出、尾进尾出

  	// 队列操作
    /**
     *	 获取队头数据项
     *	 如果头节点为null 返回null
     */
    public E peek() {
        final Node<E> f = first;
        return (f == null) ? null : f.item;
    }

    /**
     * 	获取队头数据项
     *	 如果头节为null 会抛出异常
     */
    public E element() {
        return getFirst();
    }

    /**
     * 	队头出队操作
     * 	如果头节点为null 返回null
     */
    public E poll() {
        final Node<E> f = first;
        return (f == null) ? null : unlinkFirst(f);
    }

    /**
     * 	队头出队操作
     * 	如果头节点为null 抛出异常
     */
    public E remove() {
        return removeFirst();
    }

    /**
     *	 队尾入队操作
     */
    public boolean offer(E e) {
        return add(e);
    }

    // 双端队列队列
    /**
     * 	从队头入队列
     */
    public boolean offerFirst(E e) {
        addFirst(e);
        return true;
    }

    /**
     * 	从队尾入队列
     */
    public boolean offerLast(E e) {
        addLast(e);
        return true;
    }

    /**
     *	获取队头数据
     *	如果为空返回null
     */
    public E peekFirst() {
        final Node<E> f = first;
        return (f == null) ? null : f.item;
     }

    /**
     *	获取队尾数据
     *	如果为空 返回null
     */
    public E peekLast() {
        final Node<E> l = last;
        return (l == null) ? null : l.item;
    }

    /**
     * 	从队头出队列
     * 	如果为空 返回null
     */
    public E pollFirst() {
        final Node<E> f = first;
        return (f == null) ? null : unlinkFirst(f);
    }

    /**
     *	从队尾出队列
     *	如果为空 返回null
     */
    public E pollLast() {
        final Node<E> l = last;
        return (l == null) ? null : unlinkLast(l);
    }

    /**
     * 	从队头入队列
     */
    public void push(E e) {
        addFirst(e);
    }

    /**
     *	移除头节点
     *	头节点为null会抛出一样异常
     */
    public E pop() {
        return removeFirst();
    }

    /**
     * 	移除从队头开始第一个数据为o的节点
     */
    public boolean removeFirstOccurrence(Object o) {
        return remove(o);
    }

这里只是贴出了一些比较重要的方法,想要查看更完整的LinkedList源码解析点击这里

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值