1. 数据结构--LinkedList源码摘要

public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, java.io.Serializable
{
transient int size = 0; /**
* Pointer to first node.
* Invariant: (first == null && last == null) ||
* (first.prev == null && first.item != null)
*/
transient Node<E> first; /**
* Pointer to last node.
* Invariant: (first == null && last == null) ||
* (last.next == null && last.item != null)
*/
transient Node<E> last;
}

LinkedList底层最重要的三个属性,size,first,last,可以看出,LinkedList是一个双向链表的数据结构。

 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;
}
}
Node节点包含 item元素、prev上一个节点和next下一个节点的引用。

2 LinkedList的底层数据的调整
add方法
/**
* 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;


final Node<E> newNode = new Node<>(l, e, null);


last = newNode;


if (l == null)


first = newNode;


else


l.next = newNode;


size++;


modCount++;


}

 

从源码中可以看出,在add方法中直接调用linkLast方法,该方法1.实例化一个node节点,追加到链表的末尾,2调整上一个

节点的下一个节点应用为新增节点。3修改last为新增的node节点。由此可以看出LinkedList的新增和删除操作效率明显。

get方法

/**
* Returns the element at the specified position in this list.
*
* @param index index of the element to return
* @return the element at the specified position in this list
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public E get(int index) {
checkElementIndex(index);
return node(index).item;
} /**
* Returns the (non-null) Node at the specified element index.
*/
Node<E> node(int index) {
// assert isElementIndex(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;
}
}

get方法中首先检查索引是否越界(return index >= 0 && index <= size;),第二部根据二分法判断索引是否大于size的一半,如果大于则从后往前检索,小于则从前往后检索。

Java 的 LinkedList 的底层数据结构的更多相关文章

  1. Java SE LinkedList的底层实现

    关于实现链表的底层原理 链表便于增删,不便于查询 package com.littlepage.linkedList; /** * 基于底层实现LinkedList * @author Littlep ...

  2. Java 的 ArrayList 的底层数据结构

    1. 数据结构--ArrayList源码摘要 ublic class ArrayList<E> extends AbstractList<E> implements List& ...

  3. Redis学习笔记(二)redis 底层数据结构

    在上一节提到的图中,我们知道,可以通过 redisObject 对象的 type 和 encoding 属性.可以决定Redis 主要的底层数据结构:SDS.QuickList.ZipList.Has ...

  4. Java基础知识强化之集合框架笔记29:使用LinkedList实现栈数据结构的集合代码(面试题)

    1. 请用LinkedList模拟栈数据结构的集合,并测试:  题目的意思是:     你自己的定义一个集合类,在这个集合类内部可以使用LinkedList模拟,使用LinkedList功能方法封装成 ...

  5. java 16 - 5 LinkedList模拟栈数据结构的集合

    请用LinkedList模拟栈数据结构的集合,并测试 题目的意思是: 你自己的定义一个集合类,在这个集合类内部可以使用LinkedList模拟. package cn_LinkedList; impo ...

  6. 深入解析Java对象的hashCode和hashCode在HashMap的底层数据结构的应用

    转自:http://kakajw.iteye.com/blog/935226 一.java对象的比较 等号(==): 对比对象实例的内存地址(也即对象实例的ID),来判断是否是同一对象实例:又可以说是 ...

  7. [转]java 的HashMap底层数据结构

    java 的HashMap底层数据结构   HashMap也是我们使用非常多的Collection,它是基于哈希表的 Map 接口的实现,以key-value的形式存在.在HashMap中,key-v ...

  8. java通过LinkedList实现堆栈和队列数据结构

    package shb.java.demo3; import java.util.LinkedList; public class TestLinkedList { /** * @author sha ...

  9. Java集合---LinkedList源码解析

    一.源码解析1. LinkedList类定义2.LinkedList数据结构原理3.私有属性4.构造方法5.元素添加add()及原理6.删除数据remove()7.数据获取get()8.数据复制clo ...

随机推荐

  1. render_template()的各种用法

    1.可以有很多个参数,第一个一定是模板的名字 2.可以传字典.列表.单个变量等等,还可以传函数,在模板中调用函数 后端函数: from flask import Flask from flask im ...

  2. css伪元素::before与::after使用基础示例

    1.指定文本前后添加内容 <div class="box">test</div> .box::before{ content: 'before'; marg ...

  3. Springboot JpaRepository findOne() 方法报错

    用的是springboot2.0,然后XXXRepository.findOne各种报错,各种不行,上网搜都说改回springboot1.5就好了. 这哪行,直接用XXXRepository.find ...

  4. Python说文解字_详解元类

    1.深入理解一切接对象: 1.1 什么是类和对象? 首先明白元类之前要明白什么叫做类.类是面向对象object oriented programming的重要概念.在面向对象中类和对象是最基本的两个概 ...

  5. CSS3新属性:在网站中使用访客电脑里没有安装的字体

    CSS的font-family属性使网页可以使用客户电脑里的字体,从而得到多姿多彩的WEB页面,但当客户端没有你想要使用的字体时怎么办呢?我们总不能让每个访问者都去安装一个字体吧?事实上,这是可以的! ...

  6. python语法基础-并发编程-进程-进程锁和进程间通信

    ###############   守护进程  ############## """ 守护进程 父进程中将一个子进程设置为守护进程,那么这个子进程会随着主进程的结束而结束 ...

  7. LGOJ4172 WC2006水管局长

    首先声明,这份代码空间复杂度 \(O(n^2)\),瓶颈在给边打标记 由于博主太菜,懒得再改成低复杂度的打标记了,所以\(BZOJ\)的数据过不去 Description link 给一张图,会有删边 ...

  8. 注册登录页面修订-Python使用redis-手机验证接口-发送短信验证

    登录页面修订 views.Login.vue <template> <div class="login box"> <img src="@/ ...

  9. \_\_module\_\_和\_\_class\_\_

    目录 __module__和__class__ 一.__module__ 二.通过字符导入模块 三.__class__ __module__和__class__ # lib/aa.py class C ...

  10. 吴裕雄--天生自然TensorFlow高层封装:Keras-TensorFlow API

    # 1. 模型定义. import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mnist_ ...