Java基础之LinkedList源码解析
Java集合源码解析系列
LinkedList
public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable, java.io.Serializable { transient int size = 0; /** * Node用于存储具体的数据 */ transient Node<E> first; transient Node<E> last; /** * Node中的item用于存储具体的数据 * Node中还保存了前一个节点和后一个节点 * 所以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; } } /** * LinkedList默认实现是空的 */ 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 void linkFirst(E e) { final Node<E> f = first; final Node<E> newNode = new Node<>(null, e, f); first = newNode; if (f == null) last = newNode; else f.prev = newNode; size++; modCount++; } /** * 获取头节点,而first始终指向头节点 */ public E getFirst() { final Node<E> f = first; if (f == null) throw new NoSuchElementException(); return f.item; } /** * 获取尾部节点,last始终指向尾部节点 */ public E getLast() { final Node<E> l = last; if (l == null) throw new NoSuchElementException(); return l.item; } /** * 删除数据,也就是删除节点 * 可以看出LinkedList里面可以存储null */ public boolean remove(Object o) { 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); return true; } } } return false; } /** * 删除指定位置的节点 */ public E remove(int index) { checkElementIndex(index); return unlink(node(index)); } /** * 获取指定位置节点的方法 */ Node<E> node(int index) { // 这里先判断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; } } /** * 删除节点 */ 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; size--; modCount++; return element; } /** * 检查下标是否越界,这里就是抛出IndexOutOfBoundsException的地方 */ private void checkElementIndex(int index) { if (!isElementIndex(index)) throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); } /** * 检查是否包含某个元素 */ public boolean contains(Object o) { return indexOf(o) != -1; } /** * 获取LinkedList的元素数量 */ public int size() { return size; } /** * 默认添加到链表尾部 */ public boolean add(E e) { linkLast(e); return true; } /** * 插入节点到指定位置 */ public void add(int index, E element) { checkPositionIndex(index); if (index == size) linkLast(element); else linkBefore(element, node(index)); } /** * 找出元素在链表中的位置 */ public int indexOf(Object o) { int index = 0; if (o == null) { for (Node<E> x = first; x != null; x = x.next) { if (x.item == null) return index; index++; } } else { for (Node<E> x = first; x != null; x = x.next) { if (o.equals(x.item)) return index; index++; } } return -1; } /** * 从1.5版本开始LinkedList增加了poll、peek等方法,因此LinkedList可以作为队列来使用 */ public E peek() { final Node<E> f = first; return (f == null) ? null : f.item; } public E poll() { final Node<E> f = first; return (f == null) ? null : unlinkFirst(f); } public E remove() { return removeFirst(); } /** * 从1.6版本开始,增加了push和pop方法,因此LinkedList可以作为栈来使用 */ public void push(E e) { addFirst(e); } public E pop() { return removeFirst(); } /** * LinkedList也提供了toArray方法 */ public Object[] toArray() { Object[] result = new Object[size]; int i = 0; for (Node<E> x = first; x != null; x = x.next) result[i++] = x.item; return result; } }
- LinkedList底层是用双向链表实现的,插入数据比较快,复杂度为O(1),但是对于查找和删除复杂度为O(n)(这跟直接删除节点不一样,需要先根据值遍历找到这个节点)
- 从1.5版本开始LinkedList增加了poll、peek等方法,因此LinkedList可以作为队列来使用;从1.6版本开始,增加了push和pop方法,因此LinkedList也可以作为栈来使用
- 当然,LinkedList可以保证插入元素的顺序,并且可以选择插入的顺序,默认add方法是插入到队尾
- 可以看出,LinkedList没有大小限制,默认的构造函数实现也是空的,因此不存在容量不够的情况,也没有扩容方法
- LinkedList不是线程安全的,只能用于单线程环境
以上是基于Java1.8并且只介绍了常用的一些方法的原理,详细的LinkedList源码请查看:LinkedList源码
欢迎关注我的微信公众号,和我一起学习一起成长!

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
使用go编写一个先进先出的队列,并完成帮助文档的编写、浏览、打包安装
胡说八道 Go 是一个开源的编程语言,它能让构造简单、可靠且高效的软件变得容易。 现代计算机技术的发展已经快打破英特尔戈登·摩尔提出的摩尔定律了。计算机硬件的发展速度已经远远赶不上软件技术的发展,意味着在现有硬件基础上,高效的性能才是王道。像Go、Rust等这样的语言才是未来的主导语言。 在开发人员眼中,写出完美的API很容易,可写好文档却不容易,还有各种示例程序。然而Go、Rust等这类的语言本省就带了快捷编写文档的命令。用起来也相当舒服。接下来,通过实例来演示一下Go的API文档编写。 新建工程 |--src |----queue | | queue.go | | queue_test.go |------queueentry | | main.go queue.go 编写的重点在每个类型和方法上边的注释上,正是有了编写的这些注释,才有了文档中的说明,so, 一定要写好注释! package queue // 先进先出队列 type Queue []int // 向队列中添加一个元素 // e.g. q.Push(9) func (q *Queue) Push (v int) { ...
- 下一篇
访问 cdn、oss 跨域问题
背景: 经常遇到有跨域的问题,老生长谈,却又屡禁不止,谈到跨域我们就了解下它是什么? 一句话简单说明 一个资源请求一个其它域名的资源时会发起一个跨域 HTTP 请求 (cross-origin HTTP request)。比如说,域名A(http://domaina.example) 的某 Web 应用通过<img>标签引入了域名 B(http://domainb.foo) 的某图片资源(http://domainb.foo/image.jpg),域名 A 的 Web 应用就会导致浏览器发起一个跨域 HTTP 请求 http://www.123.com/index.html 调用 http://www.123.com/server.php (非跨域) http://www.123.com/index.html 调用 http
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- CentOS8编译安装MySQL8.0.19
- Windows10,CentOS7,CentOS8安装MongoDB4.0.16
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- Red5直播服务器,属于Java语言的直播服务器
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- SpringBoot2整合Redis,开启缓存,提高访问速度
- CentOS关闭SELinux安全模块