JDK1.8源码之LinkedList

本文深入探讨JDK1.8中LinkedList的内部实现,包括其双链表结构,add、remove和get方法的工作原理,并与ArrayList进行对比。LinkedList在存储上比ArrayList更耗费空间,但增删操作更快。由于不提供线程安全,适用于需要频繁增删操作的场景。此外,LinkedList还实现了Deque接口,可用作双端队列。
摘要由CSDN通过智能技术生成

LinkedList采用双链表的数据结构,可以用作列表做存储,也可以用做双端队列。
部分属性与Node结构

	//指向列表的第一个元素
    transient Node<E> first;

	//指向列表的最后一个元素
    transient Node<E> last;
	//Node节点
    private static class Node<E> {
        E item;
        Node<E> next;//next指针
        Node<E> prev;//prev指针

        Node(Node<E> prev, E element, Node<E> next) {
            this.item = element;
            this.next = next;
            this.prev = prev;
        }
    }

add方法

    public boolean add(E e) {
        linkLast(e);
        return true;
    }
      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;//l为空,说明原来无节点,此新加入的节点既为首节点,也为尾节点
        else
            l.next = newNode;//新节点加在尾节点之后
        size++;
        modCount++;
    }

remove方法

    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;
    }
     E unlink(Node<E> x) {
        // assert x != null;
        final E element = x.item;
        final Node<E> next = x.next;
        final Node<E> prev = x.prev;

        if (prev == null) {
            first = next;
        } else {
            prev.next = next;
            x.prev = null;
        }

        if (next == null) {
            last = prev;
        } else {
            next.prev = prev;
            x.next = null;
        }

        x.item = null;
        size--;
        modCount++;
        return element;
    } 

get方法

    public E get(int index) {
        checkElementIndex(index);
        return node(index).item;
    }
     Node<E> node(int 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;
        }
    }   

与ArrayList对比

  • 存储结构不同:ArrayList使用Object数组,LinkedList使用双链表,存储同样的数据LinkedList比ArrayList更耗费空间;
  • 使用场景不同:ArrayList的随机访问效率高,但增加(扩容时元素需要进行复制)或者删除(可能存在很多元素需要移动)的效率低;LinkedList随机访问效率低,增加和删除操作快;对于少量数据或者大量但不经常增删的数据,比较适合用ArrayList,对于大量且经常需要增删的数据建议用LinkedList
  • 线程安全性:都不是线程安全的集合;
  • 实现的接口:都实现了Collection,List等接口,LinkedList还实现了Deque接口,可以用作双端队列;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值