首先看一下LinkedList基本源码,基于jdk1.8

public class LinkedList<E> extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, java.io.Serializable
{
transient int size = 0; //指向第一个节点
transient Node<E> first; //指向最后一个节点
transient Node<E> last; public LinkedList() {
} public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
  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;
   }
  }
}

从LinkedList基本代码结构,可以看出来LinkedList本质上链表

链表一般分为:单向链表、单向循环链表、双向链表、双向循环链表

LinkedList就是一个双向链表,而且实现了Deque,也可以当做双端队列使用,使用方法比较丰富

PS:JDK1.6的LinkedList为双向循环链表,之后去掉header,通过双向链表实现

一、添加:

offer()、add()和linkLast():

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,数据为e,next为null
last = newNode;  //将新节点赋值为last
if (l == null)  //如果l为null,意味着链表为空,所以置为首节点
first = newNode;
else
l.next = newNode;  //否则将新节点置为l的next节点
size++;  
modCount++;  //修改次数modCount+1
}

 

addAll():

public boolean addAll(int index, Collection<? extends E> c) {
checkPositionIndex(index); //检查下标位置是否越界 Object[] a = c.toArray(); //将Collection转换为数组
int numNew = a.length; //数组的长度numNew
if (numNew == 0)
return false; Node<E> pred, succ; //定义两个节点pred,succ。pred为插入集合的前一个节点,succ为插入集合的后一个节点
if (index == size) { //如果插入的位置等于size,将集合元素加到链表的尾部
succ = null; //succ赋值为null
pred = last; //pred赋值为last
} else {
succ = node(index); //succ赋值为index节点
pred = succ.prev; //succ的prev指向pred
} for (Object o : a) { //遍历数组
@SuppressWarnings("unchecked") E e = (E) o; //元素转换为E
Node<E> newNode = new Node<>(pred, e, null); //生成一个新节点,并且把prev指向pred
if (pred == null) //如果pred为null,newNode为首节点
first = newNode;
else
pred.next = newNode; //pred的next指向newNode
pred = newNode; //把newNode赋值为pred
} if (succ == null) { //如果插入到尾部
last = pred; pred赋值为last,pred此时为数组中最后一个元素Node
} else {
pred.next = succ; //pred的next指向succ
succ.prev = pred; //succ的prev指向pred
} size += numNew; //重新赋值size
modCount++; //修改次数增加一次
return true;
}

图中把每一步都表现出来了,不可能看不懂吧

PS:

  把Collection转换为数组的目的:toArray()保证传进来的这个集合不会被任何地方引用,也保证这个集合不会有任何机会被修改,保证了数

据的安全性

offFirst()、offLast()、addLast()和addFirst()这里就不讲了,看了上面,这里就很容易理解

二、删除:

poll()、remove()、removeFirst()和unlinkFirst():

public E remove() {
return removeFirst();
}
public E removeFirst() { //删除first
final Node<E> f = first; //得到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; //得到first节点的item
final Node<E> next = f.next; //first节点的next
f.item = null; //item置为null
f.next = null; //first节点的next置为null
first = next; //将first节点的next置为首节点
if (next == null)
last = null;
else
next.prev = null;
size--;
modCount++;
return element;
}

pollLast()、removeLast()和unlinkLast():

public E removeLast() {
final Node<E> l = last; //得到last节点
if (l == null)
throw new NoSuchElementException();
return unlinkLast(l);
}
private E unlinkLast(Node<E> l) { //释放last节点
// assert l == last && l != null;
final E element = l.item;
final Node<E> prev = l.prev;
l.item = null;
l.prev = null;
last = prev;
if (prev == null)
first = null;
else
prev.next = null;
size--;
modCount++;
return element;
}

比较简单,看下就明白了

remove(Object)和remove(index):

public boolean remove(Object o) {
if (o == null) { //如果o为null
for (Node<E> x = first; x != null; x = x.next) {
if (x.item == null) { //遍历node直到找到第一个null的index
unlink(x); //删除index节点
return true;
}
}
} else {
for (Node<E> x = first; x != null; x = x.next) {
if (o.equals(x.item)) {//遍历node直到找到第一个o的index
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) { //如果为上个节点prev等于null,直接把下个节点指向first
first = next;
} else {
prev.next = next; //prev的next赋值为下个node
x.prev = null; 当前节点的上个节点置为null
} if (next == null) { //如果删除last节点,直接把上个节点置为last节点
last = prev;
} else {
next.prev = prev; //next节点的prev指向prev节点
x.next = null; //当前节点的next置为null
} x.item = null; //当前节点item置为null,size--
size--;
modCount++;
return element;
} 

三、修改:set(index, element):

public E set(int index, E element) {
checkElementIndex(index); //检查index是否越界
Node<E> x = node(index); //获取index对应的节点
E oldVal = x.item; //获取节点的item
x.item = element; //重新赋值节点的item
return oldVal; //返回oldVal
}

四、获取:

get(index)和node(index):

public E get(int index) {
checkElementIndex(index); //检查下标
return node(index).item; //返回index对应node的item
}
Node<E> node(int index) { //使用二分法查找
if (index < (size >> 1)) { 如果index小于size的一半
Node<E> x = first; //获取first节点
for (int i = 0; i < index; i++) //因为链表不能随机访问,所以只能从0遍历到index,最终返回index对应的node
x = x.next;
return x;
} else {
Node<E> x = last; //获取last节点
for (int i = size - 1; i > index; i--) //从size-1遍历到index,最终返回index对应的node
x = x.prev;
return x;
}
}

getFirst():

public E getFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return f.item;
}

getLast():

public E getLast() {
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
return l.item;
}

contains(Object)和indexOf(Object):

public int indexOf(Object o) {
int index = 0;
if (o == null) { //如果Object为null
for (Node<E> x = first; x != null; x = x.next) { //遍历得到第一个null,返回index
if (x.item == null)
return index;
index++;
}
} else {
for (Node<E> x = first; x != null; x = x.next) { //遍历得到第一个和object相等的node.item,返回index
if (o.equals(x.item))
return index;
index++;
}
}
return -1; //如果没有,返回-1
}

for循环效率:for、foreach、lambda表达式的foreach、Iterator

测试:

public static void main(String[] args) throws IOException {
LinkedList<String> list = new LinkedList<>();
list.add("abc");
list.add("def");
for (int i = 0; i < 10000; i++) {
list.add("abc");
}
long startTime = System.currentTimeMillis();
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i) + " ");
}
System.out.println(" ");
System.out.println("普通for循环花费时间:" + (System.currentTimeMillis() - startTime));
long startTime1 = System.currentTimeMillis();
for (String s : list) {
System.out.print(s + " ");
}
System.out.println(" ");
System.out.println("foreach循环花费时间:" + (System.currentTimeMillis() - startTime1));
long startTime3 = System.currentTimeMillis();
list.forEach(s -> {
System.out.print(s + " ");
});
System.out.println(" ");
System.out.println("lambda表达式foreach循环花费时间:" + (System.currentTimeMillis() - startTime3));
long startTime2 = System.currentTimeMillis();
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
String s = (String)iterator.next();
System.out.print(s + " ");
}
System.out.println(" ");
System.out.println("Iterator循环花费时间:" + (System.currentTimeMillis() - startTime2));
}

为了结果的可信度,我们得到三次输出结果:

普通for循环花费时间:105
foreach循环花费时间:43
lambda表达式foreach循环花费时间:78
Iterator循环花费时间:31 普通for循环花费时间:97
foreach循环花费时间:47
lambda表达式foreach循环花费时间:79
Iterator循环花费时间:32 普通for循环花费时间:83
foreach循环花费时间:49
lambda表达式foreach循环花费时间:77
Iterator循环花费时间:34

普通for循环和lambda表达式foreach都很慢,Iterator最快

普通for循环最慢,应该是可以想象到的,因为LinkedList不能随机访问,每次获取都要从头到尾遍历,我们遍历10000次

虽然使用二分法可以提高效率,靠近中间的index,效率真的很慢,例如4999,5000,5001

而Iterator通过ListItr实现:

private class ListItr implements ListIterator<E> {
private Node<E> lastReturned; //本次返回的node
private Node<E> next; //本次返回的node的next节点
private int nextIndex; //下一个node对应的index
private int expectedModCount = modCount; ListItr(int index) {
next = (index == size) ? null : node(index);
nextIndex = index;
} public boolean hasNext() {
return nextIndex < size;
} public E next() {
checkForComodification();
if (!hasNext())
throw new NoSuchElementException(); lastReturned = next; //
next = next.next;
nextIndex++;
return lastReturned.item;
} }

  每次都会记录这次返回的node,下次遍历,直接取node.next,例如第4999次遍历的时候,直接返回element4998.next,而不需要像普通for循环

一样,先得到1,再是2,然后3,最后到4999

到这里,对LinkedList的了解已经差不多零,能得到的内容:

1、LinkedList由双向链表实现,add(index,element)的效率很高,只需要直接修改node的prev和next关系,但是需要new node

2、删除的时候也很快

3、不涉及到初始容量、加载因子、扩容等概念

4、不能随机访问,查询效率较慢相对于ArrayList差很多

总结:对链表的任意修改都可以归结:改变node的前后指向关系

Java集合(五)--LinkedList源码解读的更多相关文章

  1. java集合之LinkedList源码解读

    源自:jdk1.8.0_121 LinkedList继承自AbstractSequentialList,实现了List.Deque.Cloneable.Serializable. LinkedList ...

  2. 死磕 java集合之LinkedList源码分析

    问题 (1)LinkedList只是一个List吗? (2)LinkedList还有其它什么特性吗? (3)LinkedList为啥经常拿出来跟ArrayList比较? (4)我为什么把LinkedL ...

  3. 【Java集合】ArrayDeque源码解读

    简介 双端队列是一种特殊的队列,它的两端都可以进出元素,故而得名双端队列. ArrayDeque是一种以循环数组方式实现的双端队列,它是非线程安全的. 它既可以作为队列也可以作为栈. 继承体系 Arr ...

  4. Java集合:LinkedList源码解析

    Java集合---LinkedList源码解析   一.源码解析1. LinkedList类定义2.LinkedList数据结构原理3.私有属性4.构造方法5.元素添加add()及原理6.删除数据re ...

  5. Java集合之LinkedList源码分析

    概述 LinkedLIst和ArrayLIst一样, 都实现了List接口, 但其内部的数据结构不同, LinkedList是基于链表实现的(从名字也能看出来), 随机访问效率要比ArrayList差 ...

  6. Java集合之LinkedList源码解析

    LinkedList简介 LinkedList基于双向链表,即FIFO(先进先出)和FILO(先进后出)都是支持的,这样它可以作为堆栈,队列使用 继承AbstractSequentialList,该类 ...

  7. Java 集合之LinkedList源码分析

    1.介绍 链表是数据结构中一种很重要的数据结构,一个链表含有一个或者多个节点,每个节点处理保存自己的信息之外还需要保存上一个节点以及下一个节点的指针信息.通过链表的表头就可以访问整个链表的信息.Jav ...

  8. java集合之ArrayList源码解读

    源自:jdk1.8.0_121 ArrayList继承自AbstractList,实现了List.RandomAccess.Cloneable.Serializable. ArrayList内部是通过 ...

  9. Java集合干货——LinkedList源码分析

    前言 在上篇文章中我们对ArrayList对了详细的分析,今天我们来说一说LinkedList.他们之间有什么区别呢?最大的区别就是底层数据结构的实现不一样,ArrayList是数组实现的(具体看上一 ...

随机推荐

  1. cmake官方文档

    https://cmake.org/cmake/help/v3.0/genindex.html

  2. LeNet-5结构分析及caffe实现————卷积部分

    占坑,记录 1.lenet-5的结构以及部分原理 2.caffe对于lenet-5的代码结构 图一 图一是整个LeNet-5的结构图,要点有:convolutions.subsampling.full ...

  3. UVaLive 7454 Parentheses (水题,贪心)

    题意:给定一个括号序列,改最少的括号,使得所有的括号匹配. 析:贪心,从左到右扫一下,然后统计一下左括号和右括号的数量,然后在统计中,如果有多了的右括号,那么就改成左括号,最后如果两括号数量不相等, ...

  4. KDotAlert

    一个iPhone X的适配让楼主受尽了自定义的苦,使用系统API多好,所以在楼主不懈的努力下,终于和组长达成一致:逐步用系统控件替换代码里面的自定义控件,第一个挨刀的就是 BlockAlertsAnd ...

  5. ORACLE PL/SQL 实例精解之第五章 条件控制:CASE语句

    5.1 CASE语句 1. CASE语句具有如下结构 CASE SELECTOR WHEN EXPRESSION 1 THEN STATEMENT 1; WHEN EXPRESSSION 2 THEN ...

  6. Mac下的常用终端命令与vim常用命令

    因为很少用命令行,老被鄙视,所以今天记录一下常用的命令行: cd 切换工作目录 . 表示当前目录 .. 表示当前目录的上一级目录 / 根目录/目录分隔符 ./ 当前目录 ../ 回到上一级目录 ls ...

  7. 洛谷 P4125 [WC2012]记忆中的水杉树【扫描线+set+线段树】

    我没有找到能在bzojAC的代码--当然我也WA了--但是我在洛谷过了,那就假装过了吧 minmax线段树一开始写的只能用min更新min,max更新max,实际上是可以互相更新的-- 首先看第二问, ...

  8. win32 寄存器

    跳转指令分三类: 一.无条件跳转: JMP ;无条件跳转 二.根据CX.ECX寄存器的值跳转: JCXZ ;CX 为 0 则跳转 JECXZ;ECX 为 0 则跳转 三.根据EFLAGS寄存器的PSW ...

  9. set有关的函数的用法(The SetStack Computer UVA - 12096)

    #include<bits/stdc++.h> using namespace std; typedef set<int> Set; map<Set,int> ID ...

  10. 题解报告:hdu 2141 Can you find it?(二分)

    Problem Description Give you three sequences of numbers A, B, C, then we give you a number X. Now yo ...