JavaGuide项目:深入解析LinkedList源码实现

JavaGuide项目:深入解析LinkedList源码实现

JavaGuide JavaGuide:这是一份Java学习与面试指南,它涵盖了Java程序员所需要掌握的大部分核心知识。这份指南是一份通俗易懂、风趣幽默的学习资料,内容全面,深受Java学习者的欢迎。 JavaGuide 项目地址: https://gitcode.com/gh_mirrors/ja/JavaGuide

引言

LinkedList是Java集合框架中一个重要的双向链表实现,它实现了List和Deque接口。本文将深入分析LinkedList的底层实现原理,帮助读者全面理解这一数据结构的特性和使用场景。

LinkedList基本特性

LinkedList是基于双向链表实现的数据结构,与ArrayList相比具有以下特点:

  1. 内存结构:元素在内存中非连续存储,通过指针连接各个节点
  2. 访问效率:随机访问效率低(O(n)),顺序访问效率高
  3. 增删效率:头尾操作效率高(O(1)),中间操作效率低(O(n))

时间复杂度分析

| 操作 | 时间复杂度 | 说明 | |------|-----------|------| | 头部插入/删除 | O(1) | 直接操作头节点 | | 尾部插入/删除 | O(1) | 直接操作尾节点 | | 指定位置插入/删除 | O(n) | 需要遍历到指定位置 | | 随机访问 | O(n) | 需要从头或尾遍历 |

核心源码解析

类结构与节点定义

LinkedList的类继承关系如下:

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

内部使用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;
    }
}

关键操作实现

1. 添加元素

LinkedList提供了多种添加元素的方式:

// 尾部添加
public boolean add(E e) {
    linkLast(e);
    return true;
}

// 指定位置添加
public void add(int index, E element) {
    checkPositionIndex(index);
    if (index == size)
        linkLast(element);
    else
        linkBefore(element, node(index));
}

其中linkLastlinkBefore是核心方法:

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

void linkBefore(E e, Node<E> succ) {
    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++;
}
2. 获取元素

获取元素的核心方法是node(int index),它采用二分查找优化:

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;
    }
}
3. 删除元素

删除操作的核心是unlink方法:

E unlink(Node<E> x) {
    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;
}

迭代器实现

LinkedList提供了双向迭代器ListItr,支持正向和反向遍历:

private class ListItr implements ListIterator<E> {
    private Node<E> lastReturned;
    private Node<E> next;
    private int nextIndex;
    private int expectedModCount = modCount;
    
    // 正向遍历
    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;
    }
}

使用建议

  1. 适用场景

    • 频繁在头尾进行插入删除操作
    • 不需要频繁随机访问元素
    • 需要实现栈、队列或双端队列
  2. 不适用场景

    • 需要频繁随机访问元素
    • 内存空间有限的情况(相比ArrayList有额外指针开销)
  3. 最佳实践

    • 使用迭代器而不是get(index)遍历
    • 优先使用addFirst/addLast等方法进行头尾操作
    • 考虑使用ArrayDeque作为替代(性能更好)

总结

LinkedList作为Java集合框架中重要的链表实现,其双向链表的特性使其在特定场景下具有优势。通过本文的源码分析,我们深入理解了它的内部实现机制和工作原理。在实际开发中,应根据具体需求选择合适的集合实现,LinkedList适合频繁插入删除但不频繁随机访问的场景。

JavaGuide JavaGuide:这是一份Java学习与面试指南,它涵盖了Java程序员所需要掌握的大部分核心知识。这份指南是一份通俗易懂、风趣幽默的学习资料,内容全面,深受Java学习者的欢迎。 JavaGuide 项目地址: https://gitcode.com/gh_mirrors/ja/JavaGuide

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

诸盼忱Gazelle

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值