链表(Linkedlist)是一种常见的基础数据结构,是一种线性表,但是并不会按线性的顺序存储数据,而是在每一个节点里存到下一个节点的地址。链表可分为单向链表和双向链表
1.LinkedList是基于链表的,而且是一个双向链表,不需要连续内存空间。
//可以看出Node是一个双链表结构
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;
}
}
2.对于add操作,有两种情况,
1)如果不指定位置,则在末尾添加。只需改变指针指向即可。
//尾插
void linkLast(E e) {
//保存的链表的尾部节点
final Node<E> l = last;
//新的节点的前一个node指向链表的最后一个,next 为null
final Node<E> newNode = newNode < E > (l, e, null);
last = newNode;
if (l == null)//如果最后一个节点为null,说明链表是空的
first = newNode;
else//如果不为null,last的next节点指向新的节点
l.next = newNode;
size++;
modCount++;
}
2)如果在指定位置添加,则需要遍历链表。如果索引小于链表长度的一半,从头遍历,否则从尾部遍历。调用node(index)进行遍历。
public void add(int index, E element) {
if (index == size)//新插入节点刚好在链表尾部
linkLast(element);
else
linkBefore(element, 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 { //如果index大于链表的一半,则尾部遍历
Node<E> x = last;
for (int i = size - 1; i > index; i--)
x = x.prev;
return x;
}
}
void linkBefore(E e, Node<E> succ) {
// 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++;
}
3.对于get和set方法都需要遍历链表,相比ArrayList,效率要低。
public E get(int index) {
checkElementIndex(index);
return node(index).item;
}
public E set(int index, E element) {
checkElementIndex(index);
Node<E> x = node(index);
E oldVal = x.item;
x.item = element;
return oldVal;
}
4.对于remove,则需要遍历整个链表
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;
}
unlink 断开链表中被删节点前后的指向,建立新的指向。
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;
}
5.ArrayList分析
ArrayList源码分析
6.总结:
1)对于get和set,Arraylist效率高,直接通过索引取值,而LinkedList,则需要遍历链表。
2)对于Add方法,
如果添加到末尾,差不多。
如果插入在中间,ArrayList需要移动素组,而LinkedList只需操作指针即可,效率要高。但是LinkedList,需要通过索引查找到要插入的位置。
3)对于remove方法。
如果删除在尾部,差不多。
如果删除中间某个元素,ArrayList则需要移动数组。而Linkedlist如果删除的是对象只需要操作指针即可,如果是按索引删除,则需要遍历,找到该元素。
到此这篇关于Java LinkedList源码深入分析的文章就介绍到这了,更多相关Java LinkedList内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!
本文标题为:Java LinkedList源码深入分析


基础教程推荐
- 存储 20 位数字的数据类型 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01