Java-集合( LinkedList底层分析)

LinkedList底层分析

源码场景

java 复制代码
LinkedList<String> list = new LinkedList<>();
		
list.add("小宇");
list.add("小蒲");
list.add("小康");

源码分析

java 复制代码
public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
    //外部操作数
    protected transient int modCount = 0;
}
java 复制代码
public abstract class AbstractSequentialList<E> extends AbstractList<E> {
}
java 复制代码
public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>{
    //元素个数
    transient int size = 0;//0
    //第一个节点
    transient Node<E> first;//null
    //最后一个节点
    transient Node<E> last;//null
    
    public LinkedList() {
    }
    
    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;
        else
            l.next = newNode;
        size++;
        modCount++;
    }
    
    //节点类
    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;
        }
    }
}
相关推荐
人活一口气14 小时前
Spring Boot与AIGC的完美结合:从零搭建智能内容生成平台
java·spring boot·aigc
像我这样帅的人丶你还16 小时前
Java 后端详解(三):全局异常处理与 JPA 数据库映射
java·后端
NE_STOP16 小时前
vibe Coding -- 小项目实战
java
未秃头的程序猿1 天前
Java 26正式发布!这3个新特性,让代码量直接减半
java·后端·面试
用户298698530141 天前
Word 文档文本查找与替换的 Java 实现方案
java·后端
阿哉1 天前
Nacos 服务发现源码:藏在背后的两套事件机制,90%的人只讲了一半
java
咖啡八杯1 天前
GoF设计模式——命令模式
java·设计模式·架构
AI人工智能_电脑小能手1 天前
【大白话说Java面试题 第125题】【并发篇】第25题:说说 Java 线程的中断机制
java·后端·面试
Java内核笔记1 天前
Spring Security 源码解析(六)无状态 JWT 实践:Session 共享与自定义过滤器
java·后端
荣码1 天前
LangGraph多Agent协作:3个Agent干活比1个强,但我踩了4个坑
java·python