以脑图的形式来展示Java集合知识,让零碎知识点形成体系

LinkedList

   LinkedList是一种可以在任何位置进行高效地插入和删除操作的有序序列。
   它的最基本存储结构是一个节点:每个节点将存储对象,以及前后节点的引用。

结构图

 
LinkedList 结构体

   从上面的结构图中,我们可以了解到 ListedList 底层是基于双向链表实现的。
   围起来的可以看成 LinkedList 类,它定义了三个 transient 成员变量:first、last、size。这三个变量是整个 LinkedList 类的关键点。

  1. 由于是双向链表(每个node都有保存前后节点的引用),因此我们不管是由 first 还是 last 节点开始迭代,都可以将整个链表的数据找出来;
  2. 在查询、随机插入以及set等操作都有涉及 size 判断;
  3. 由于 LinkedList 是双向链表,类中只存储了首尾两个节点,因此查询第n个元素都要从头遍历进行查找。

源码分析

  add(E e)  源码分析

     /**
* Appends the specified element to the end of this list.
*
* <p>This method is equivalent to {@link #addLast}.
*
* @param e element to be appended to this list
* @return {@code true} (as specified by {@link Collection#add})
*/
public boolean add(E e) {
linkLast(e);
return true;
} /**
* Links e as last element.
*/
void linkLast(E e) {
final Node<E> l = last; // 将当前最后一个元素寄存在 l
final Node<E> newNode = new Node<>(l, e, null); // new 一个新节点:pre的引用为l;存储元素为e;next的引用为null
last = newNode; // 将新节点引用覆盖成员变量 last
if (l == null)
first = newNode; // 若l为null,说明之前链表为空,此时新节点为首个元素
else
l.next = newNode; // 否则,更新l的next引用
size++; // size+1
modCount++; // 非查询操作 modCount 都会 +1
}

  add(int index, E element) 方法分析

     /**
* Inserts the specified element at the specified position in this list.
* Shifts the element currently at that position (if any) and any
* subsequent elements to the right (adds one to their indices).
*
* @param index index at which the specified element is to be inserted
* @param element element to be inserted
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public void add(int index, E element) {
checkPositionIndex(index); // 检查 index 是否大于 size if (index == size)
linkLast(element); // 直接在链表末尾追加
else
linkBefore(element, node(index)); // 插入index 节点前面
} // 检查 index 是否超出范围 超出则抛出 IndexOutOfBoundsException
private void checkPositionIndex(int index) {
if (!isPositionIndex(index))
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
} /**
* Tells if the argument is the index of a valid position for an
* iterator or an add operation.
*/
private boolean isPositionIndex(int index) {
return index >= 0 && index <= size;
} /**
* 根据 index 查找 node
* 该方法利用了双向链表的特性,index 距离哪个链表头近就从哪边开始开始遍历
* 时间复杂度为 O(n/2);
* 当 index 接近 size 的中间值时,效率最低
* Returns the (non-null) Node at the specified element index.
*/
Node<E> node(int index) {
// assert isElementIndex(index); if (index < (size >> 1)) { // size 右移一位(除以2)
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;
}
}

优缺点

优点

  • 增删元素效率高(只需要更新节点附近的引用即可)

缺点

  • 由于查询需要进行遍历,因此效率低

知识脑图

From Java Core Knowledge Tree

 
LinkedList 脑图
 

  在 github 上建了一个 repository ,Java Core Knowledge Tree,各位看官若是喜欢请给个star,以示鼓励,谢谢。
https://github.com/suifeng412/JCKTree

(以上是自己的一些见解,若有不足或者错误的地方请各位指出)

作者:那一叶随风   http://www.cnblogs.com/phpstudy2015-6/

原文地址: https://www.cnblogs.com/phpstudy2015-6/p/10626564.html

声明:本博客文章为原创,只代表本人在工作学习中某一时间内总结的观点或结论。转载时请在文章页面明显位置给出原文链接

Java 集合系列(三)—— LinkedList的更多相关文章

  1. Java 集合系列 04 LinkedList详细介绍(源码解析)和使用示例

    java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...

  2. java集合系列之LinkedList源码分析

    java集合系列之LinkedList源码分析 LinkedList数据结构简介 LinkedList底层是通过双端双向链表实现的,其基本数据结构如下,每一个节点类为Node对象,每个Node节点包含 ...

  3. Java集合系列(三):HashSet、LinkedHashSet、TreeSet的使用方法及区别

    本篇博客主要讲解Set接口的三个实现类HashSet.LinkedHashSet.TreeSet的使用方法以及三者之间的区别. 注意:本文中代码使用的JDK版本为1.8.0_191 1. HashSe ...

  4. 【java集合系列】--- LinkedList

    开篇前言--LinkedList中的基本用法 在前面的博文中,小编介绍List接口中的ArrayList集合,List这个接口,有两个实现类,一个就是ArrayList另一个是LinkedList(链 ...

  5. Java集合系列[2]----LinkedList源码分析

    上篇我们分析了ArrayList的底层实现,知道了ArrayList底层是基于数组实现的,因此具有查找修改快而插入删除慢的特点.本篇介绍的LinkedList是List接口的另一种实现,它的底层是基于 ...

  6. 【Java集合系列三】Vector-Stack解析

    2017-07-29 12:59:14 一.简介 1.Vector继承关系 2.Vector类扩容 Vector类的实现和ArrayList极其相似,都使用数组存储元素,但是扩容策略不一样,Array ...

  7. 深入理解JAVA集合系列三:HashMap的死循环解读

    由于在公司项目中偶尔会遇到HashMap死循环造成CPU100%,重启后问题消失,隔一段时间又会反复出现.今天在这里来仔细剖析下多线程情况下HashMap所带来的问题: 1.多线程put操作后,get ...

  8. Java 集合系列 07 List总结(LinkedList, ArrayList等使用场景和性能分析)

    java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...

  9. Java 集合系列 12 TreeMap

    java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...

  10. Java 集合系列 08 Map架构

    java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...

随机推荐

  1. Javaweb编程中的乱码问题

    程序中的乱码问题,主要出现在我们处理中文数据的过程中出现.从浏览器向服务器请求数据,服务器返回的数据在浏览器中显示为乱码.或者是服务器中的java文件用到中文,也有可能会出现乱码.数据库在处理数据的时 ...

  2. github pages绑定域名

    网上很多人问 github 绑定域名要不要备案,很多人的回答是: 国内主机需要备案,国外主机不用 这个说法是没错的,但是却没有直接回答出 github pages 是否需要备案! 首先声明 githu ...

  3. 有了iron-node,调试Nodejs就是怎么so easy

    全局安装 iron-node工具 cnpm install -g iron-node 运行iron-node iron-node ./build/dev-server.js 调试效果图

  4. 在越狱的iPhone/iPad上安装自开发环境

    自开发跟自编译意思一样,后者表示一个开发语言的开发能力成熟度:前者则表示一个开发平台的开发能力成熟度. iPhone跟iPad面世这么多年,一直无法摆脱"娱乐"工具的宿命.Appl ...

  5. Spring RestTemplate中几种常见的请求方式

    https://github.com/lenve/SimpleSpringCloud/tree/master/RestTemplate在Spring Cloud中服务的发现与消费一文中,当我们从服务消 ...

  6. Eclipse导入别人的项目报错:Unable to load annotation processor factory 'xxxxx.jar' for project

    使用eclipse导入别人的项目时候,报错Unable to load annotation processor factory 'xxxxx.jar' for project. 解决方案 1.项目右 ...

  7. leetcode — remove-duplicates-from-sorted-array-ii

    /** * Source : https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ * * * Follow ...

  8. Linux系统命令行整理

    1.pwd  查看当前所在目录 2.cd /  跳往根目录 3.ls  查看当前目录所有子目录或文件 4.ls -l  列出当前目录详细信息 5.ls -lh  h=human 人性化列出当前目录详细 ...

  9. C# 如何在PDF中绘制不同风格类型的文本

    通过对控件Spire.PDF的测试,我们可以创建PDF文件并向文档中绘制文本.图片.表格.图形等内容,其中,对于绘制文本这一部分,Spire.PDF提供了三种字体类型来绘制文本,即: Standard ...

  10. Linux下GitLab服务器搭建

    系统环境 操作系统:CentOS6.9关闭防火墙 安装步骤 1. 安装Postfix 2. 下载rpm包并安装 3. 配置gitlab,vim /etc/gitlab/gitlab.rb,指定ip+端 ...