总结:

  1. LinkedList继承自List,具备有序性
  2. LinkedList继承自Deque,具备链表关联性
  3. LinkedList集合进行增删改查操作底层实际是操作Node节点的前后链接关系
  4. LinkedList进行增删操作时,仅需要操作节点的前后链接关系,因此效率较ArrayList高
  5. LinkedList进行查找操作时,必须从头或者从尾进行查找,因此较底层依靠数组进行存储的ArrayList查找效率低

示例代码

public class LinkedList01 {
public static void main(String[] args) {
LinkedList linkedList = new LinkedList(); //执行第1步
linkedList.add(1); //执行第2步
linkedList.add(2); //执行第3步
linkedList.add(3); //执行第4步
linkedList.add(1 , new Intger(8)); //执行第8步
linkedList.add(5);
linkedList.remove(); //执行第5步
linkedList.remove(2); //执行第6步
linkedList.remove(new Integer(3)); //执行第7步
System.out.println(linkedList);
}
}

底层代码

第1步(初始化集合)

//LinkedList类默认构造器
public LinkedList() {}
transient int size = 0; //集合存放对象个数
transient Node<E> first; //集合中第一个节点
transient Node<E> last; //集合中最后一个节点
...
//AbstractSequentialList类默认构造器
protected AbstractSequentialList() {}
...
//AbstractList类默认构造器
protected AbstractList() {}
protected transient int modCount = 0;
...
//AbstractCollection类默认构造器
protected AbstractCollection() {}
...
//Object类默认构造器
public Object() {}

结果:还没有存放对象,属于空集合


第2步(往集合中添加一个元素)

public boolean add(E e) {  //e = 1
linkLast(e);
return true;
}
...
void linkLast(E e) {
final Node<E> l = last;//l = null
final Node<E> newNode = new Node<>(l, e, null);//创建新的节点,当前节点的prev和next属性均为null,将存入集合的对象赋值给item
last = newNode;//LinkedList集合的last属性指向新节点
if (l == null)//此时i=null,条件成立
first = newNode;//LinkedList集合的first属性指向新节点
else
l.next = newNode;
size++;//LinkedList集合的容量自加1
modCount++;//LinkedList集合修改次数自加1
}
......
//Node是LinkedList类的内部类
private static class Node<E> {
E item; //LinkedLIst实际存放的对象
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类中first与last属性相同,Node类中prev与next属性为null


第3步(往集合中添加第二个元素)

public boolean add(E e) {  //e = 2
linkLast(e);
return true;
}
...
void linkLast(E e) {
final Node<E> l = last;//l = 1,表示上一个节点
final Node<E> newNode = new Node<>(l, e, null);//创建新的节点,节点的prev属性指向上一个节点,item属性存放当前对象
last = newNode;//LinkedList集合的last属性指向新节点
if (l == null)//此时i!=null,条件不成立
first = newNode;
else
l.next = newNode;//上一个节点的next属性指向当前节点,即新创建的节点
size++;//LinkedList集合的容量自加1
modCount++;//LinkedList集合修改次数自加1
}

结果:


第4步(往集合中添加第三个元素)

public boolean add(E e) {  //e = 3
linkLast(e);
return true;
}
...
void linkLast(E e) {
final Node<E> l = last;//l = 2,表示上一个节点
final Node<E> newNode = new Node<>(l, e, null);//创建新的节点,节点的prev属性指向上一个节点,
item属性存放当前对象
last = newNode;//LinkedList集合的last属性指向新节点
if (l == null)//此时i!=null,条件不成立
first = newNode;
else
l.next = newNode;//上一个节点的next属性指向当前节点,即新创建的节点
size++;//LinkedList集合的容量自加1
modCount++;//LinkedList集合修改次数自加1
}

结果:


LinkedList添加元素流程示意图


第5步(删除集合中第一个元素)

public E remove() {
return removeFirst();
}
...
public E removeFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return unlinkFirst(f);
}
...
private E unlinkFirst(Node<E> f) {
// assert f == first && f != null;
final E element = f.item; //将集合中第一个节点的item 属性赋值给element
final Node<E> next = f.next; //将集合中第一个节点的next属性赋值给next
f.item = null;
f.next = null; // help GC
first = next; //将原集合中的第二个节点赋给集合的first属性
if (next == null)
last = null;
else
next.prev = null;//将原集合中的第二个节点的prev属性赋值为null
size--; //集合元素个数自减1
modCount++; //集合修改次数自加1
return element; //返回被删除的节点item值
}


第6步(根据索引来删除集合中的元素)

public E remove(int index) { //index = 2
checkElementIndex(index); //1.嵌套执行下边两个方法①和②,确定索引正确后继续往下执行
return unlink(node(index)); //2.执行方法③与④
}
...
//方法①
private void checkElementIndex(int index) {
if (!isElementIndex(index))
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
...
//方法②
private boolean isElementIndex(int index) {
return index >= 0 && index < size;
}
...
//方法③
Node<E> node(int index) { //index = 2, size = 4
// assert isElementIndex(index);
if (index < (size >> 1)) { //index < size/2时
Node<E> x = first; //x记录首个节点
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;
}
}
...
//方法④
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; //此时该节点中的属性item、prev、next均为null
size--; //集合元素个数自减1
modCount++; //集合修改次数自加1
return element; //返回被删除节点中的内容
}

第7步(根据对象内容来删除集合中的元素)

//本方法可以用来删除集合中对象和null
public boolean remove(Object o) { o = new Integer(3)
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); //调用方法与第6步中流程一致
return true;
}
}
}
return false;
}

第8步(根据索引位置往集合中添加元素)

public void add(int index, E element) { //index=1, element = new Integer(8)
checkPositionIndex(index); //检查索引没有问题 if (index == size) //如果索引与集合大小相等
linkLast(element);
else
linkBefore(element, node(index)); //node(index)方法找到该索引位置的节点,然后采用linkBefore方法在其节点前链接入新的节点
}
...
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) { //e = new Integer(8)待链接入的节点,succ为原index位置的节点
// assert succ != null;
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++;
}

集合框架——LinkedList集合源码分析的更多相关文章

  1. List-LinkedList、set集合基础增强底层源码分析

    List-LinkedList 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 继上一章继续讲解,上章内容: List-ArreyLlist集合基础增强底层源码分析:https:// ...

  2. List-ArrayList集合基础增强底层源码分析

    List集合基础增强底层源码分析 作者:Stanley 罗昊 [转载请注明出处和署名,谢谢!] 集合分为三个系列,分别为:List.set.map List系列 特点:元素有序可重复 有序指的是元素的 ...

  3. Java集合系列[4]----LinkedHashMap源码分析

    这篇文章我们开始分析LinkedHashMap的源码,LinkedHashMap继承了HashMap,也就是说LinkedHashMap是在HashMap的基础上扩展而来的,因此在看LinkedHas ...

  4. java集合系列之ArrayList源码分析

    java集合系列之ArrayList源码分析(基于jdk1.8) ArrayList简介 ArrayList时List接口的一个非常重要的实现子类,它的底层是通过动态数组实现的,因此它具备查询速度快, ...

  5. DotNetty网络通信框架学习之源码分析

    DotNetty网络通信框架学习之源码分析 有关DotNetty框架,网上的详细资料不是很多,有不多的几个博友做了简单的介绍,也没有做深入的探究,我也根据源码中提供的demo做一下记录,方便后期查阅. ...

  6. LinkedList 的源码分析

    LinkedList是基于双向链表数据结构来存储数据的,以下是对LinkedList  的 属性,构造器 ,add(E e),remove(index),get(Index),set(inde,e)进 ...

  7. Java集合系列:-----------03ArrayList源码分析

    上一章,我们学习了Collection的架构.这一章开始,我们对Collection的具体实现类进行讲解:首先,讲解List,而List中ArrayList又最为常用.因此,本章我们讲解ArrayLi ...

  8. Java集合:HashSet的源码分析

    Java集合---HashSet的源码分析   一.  HashSet概述: HashSet实现Set接口,由哈希表(实际上是一个HashMap实例)支持.它不保证set 的迭代顺序:特别是它不保证该 ...

  9. java集合框架03——ArrayList和源码分析

    最近忙着替公司招人好久没写了,荒废了不好意思. 上一章学习了Collection的架构,并阅读了部分源码,这一章开始,我们将对Collection的具体实现进行详细学习.首先学习List.而Array ...

  10. 深入理解分布式调度框架TBSchedule及源码分析

    简介 由于最近工作比较忙,前前后后花了两个月的时间把TBSchedule的源码翻了个底朝天.关于TBSchedule的使用,网上也有很多参考资料,这里不做过多的阐述.本文着重介绍TBSchedule的 ...

随机推荐

  1. fill-available,min-content,max-content,fit-content的作用机制

    fill-available:宽度由外部元素决定(div)min-content:宽度由内部元素宽度缩小到最小的最大内部元素宽度决定max-content:宽度由内部元素宽度扩大到最大后的最大内部元素 ...

  2. Vue 监视数据总结 && 表单控件使用总结

    1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8" /> 5 & ...

  3. Linux Shell 自动交互功能

    需求背景:   近日,在安装某软件过程,发现在安装过程需要输入一些信息才能继续下一步操作,在机器数量较少情况下,我们可以单台登录上去完成安装操作,但当机器数量超过一定时,如果再手动登录操作,就会产生大 ...

  4. 如何自定义一个Collector

    Collectors类提供了很多方便的方法,假如现有的实现不能满足需求,我们如何自定义一个Collector呢?   Collector接口提供了一个of方法,调用该方法就可以实现定制Collecto ...

  5. Redis进阶篇:发布订阅模式原理与运用

    "65 哥,如果你交了个漂亮小姐姐做女朋友,你会通过什么方式将这个消息广而告之给你的微信好友?" "那不得拍点女朋友的美照 + 亲密照弄一个九宫格图文消息在朋友圈发布大肆 ...

  6. .NET 6应用程序适配国产银河麒麟V10系统随记

    最近想在麒麟系统上运行.NET 6程序,经过一番折腾最终完成了,简单记录一下. 目标系统: CPU: aarch64架构(ARM64) 操作系统:银河麒麟V10高级服务器系统 银河麒麟V10系统(以下 ...

  7. centos 安装ftp服务BUG

    安装完成之后匿名可登录,但是先创建的用户名和密码无法登录,最后排查原因是/etc/pam.d/vsftpd 文件注释掉第四行 auth required pam_shells.so

  8. C#.NET ORM FreeSql 读取使用 US7ASCII 的 Oracle 数据库中文显示乱码问题

    前言 关于 Oracle US7ASCII 中文乱码的问题,Ado.Net 和 Odbc 无法解决.包括最新的.Net Core..NET6..NET7 都无法解决这个问题. FreeSql 对 Or ...

  9. OpenCV CMake VSCode Windows 平台下运行配置及其解决方案

    前言 最近在搞 计算机图形学相关的东西,有个 demo 用到了 opencv,找了 google 一圈,发现国内都没有比较好的配置和解决的办法,要不就是几年前的教程,最近正好踩坑完,其中经历了自己编译 ...

  10. [RootersCTF2019]I_<3_Flask-1|SSTI注入

    1.打开之后很明显的提示flask框架,但是未提供参数,在源代码中发现了一个git地址,打开之后也是没有啥用,结果如下: 2.这里我们首先需要爆破出来参数进行信息的传递,就需要使用Arjun这一款工具 ...