public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, java.io.Serializable

LinkedList是一个双向链表的实现,允许所有的元素,包括null。

对于index操作,会从链头到链尾地搜索,即使它靠近一个特殊索引位置。

非线程安全的,在多线程环境下,需要外部同步或调用Collections.synchronizedList(new LinkedList(...));获得一个线程安全的链表。

迭代器是快速失败的。

3个实例变量

//链表大小
transient int size = 0; //链头
transient Node<E> first; //链尾
transient Node<E> last;

2个构造器

//空链表
public LinkedList() {
} //根据c创建链表,链表的顺序取决于c的iterator返回元素的顺序
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}

操作

//返回链表第一个元素
public E getFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return f.item;
} //返回链表最后一个元素
public E getLast() {
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
return l.item;
} //删除第一个元素,返回值是第一个元素的值
public E removeFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return unlinkFirst(f);
} //删除最后一个元素,返回值是最后一个元素
public E removeLast() {
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
return unlinkLast(l);
} //插入一个元素作为链头
public void addFirst(E e) {
linkFirst(e);
} //插入一个元素作为链尾
public void addLast(E e) {
linkLast(e);
} //判断链表是否含有o
public boolean contains(Object o) {
return indexOf(o) != -1;
} //返回链表大小
public int size() {
return size;
} //与addLast的效果一样,在链尾添加一个元素
public boolean add(E e) {
linkLast(e);
return true;
} //删除从链头开始遇到第一个与o等同的元素,如果返回true,表示删除成功
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;
} //将c集合的元素添加到链表,插入顺序取决于c的iterator返回的元素顺序
public boolean addAll(Collection<? extends E> c) {
return addAll(size, c);
} //在index位置开始将c集合的元素添加到链表,插入顺序取决于c的iterator返回的元素顺序
public boolean addAll(int index, Collection<? extends E> c) {
checkPositionIndex(index); Object[] a = c.toArray();
int numNew = a.length;
if (numNew == 0)
return false; Node<E> pred, succ;
if (index == size) {
succ = null;
pred = last;
} else {
succ = node(index);
pred = succ.prev;
} for (Object o : a) {
@SuppressWarnings("unchecked") E e = (E) o;
Node<E> newNode = new Node<>(pred, e, null);
if (pred == null)
first = newNode;
else
pred.next = newNode;
pred = newNode;
} if (succ == null) {
last = pred;
} else {
pred.next = succ;
succ.prev = pred;
} size += numNew;
modCount++;
return true;
} //清空链表
public void clear() {
// Clearing all of the links between nodes is "unnecessary", but:
// - helps a generational GC if the discarded nodes inhabit
// more than one generation
// - is sure to free memory even if there is a reachable Iterator
for (Node<E> x = first; x != null; ) {
Node<E> next = x.next;
x.item = null;
x.next = null;
x.prev = null;
x = next;
}
first = last = null;
size = 0;
modCount++;
} //获取在index位置的元素
public E get(int index) {
checkElementIndex(index);
return node(index).item;
} //将index位置的元素的值修改为element
public E set(int index, E element) {
checkElementIndex(index);
Node<E> x = node(index);
E oldVal = x.item;
x.item = element;
return oldVal;
} //在index位置插入一个element元素
public void add(int index, E element) {
checkPositionIndex(index); if (index == size)
linkLast(element);
else
linkBefore(element, node(index));
} //删除index位置的元素,返回值是被删除元素的值
public E remove(int index) {
checkElementIndex(index);
return unlink(node(index));
} //返回从链头到链尾第一个遇到与o等同的元素所在的位置,如果不存在返回-1
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;
} //返回从链尾到链头第一个遇到与o等同的元素所在的位置,如果不存在返回-1
public int lastIndexOf(Object o) {
int index = size;
if (o == null) {
for (Node<E> x = last; x != null; x = x.prev) {
index--;
if (x.item == null)
return index;
}
} else {
for (Node<E> x = last; x != null; x = x.prev) {
index--;
if (o.equals(x.item))
return index;
}
}
return -1;
} //返回链头元素值
public E peek() {
final Node<E> f = first;
return (f == null) ? null : f.item;
} //返回链头元素值,如果链表为空,抛出NoSuchElementException
public E element() {
return getFirst();
} //返回并删除链头元素值
public E poll() {
final Node<E> f = first;
return (f == null) ? null : unlinkFirst(f);
} //返回并删除链头元素值,如果链表为 空,抛出NoSuchElementException
public E remove() {
return removeFirst();
} //在链尾添加元素e
public boolean offer(E e) {
return add(e);
} //在链头插入元素e
public boolean offerFirst(E e) {
addFirst(e);
return true;
} //在链尾插入元素e
public boolean offerLast(E e) {
addLast(e);
return true;
} //返回链头元素,如果链表为空,返回null
public E peekFirst() {
final Node<E> f = first;
return (f == null) ? null : f.item;
} //返回链尾元素,如果链表为空,返回null
public E peekLast() {
final Node<E> l = last;
return (l == null) ? null : l.item;
} //返回并删除链头元素
public E pollFirst() {
final Node<E> f = first;
return (f == null) ? null : unlinkFirst(f);
} //返回并删除链尾元素
public E pollLast() {
final Node<E> l = last;
return (l == null) ? null : unlinkLast(l);
} //在链头插入元素e
public void push(E e) {
addFirst(e);
} //删除并返回链头元素
public E pop() {
return removeFirst();
} //删除从链头到结尾第一个遇到与o等同的元素,如果不存在,什么也不做
public boolean removeFirstOccurrence(Object o) {
return remove(o);
} //删除从链尾到链头第一个遇到与o等同的元素,如果不存在,什么也不做
public boolean removeLastOccurrence(Object o) {
if (o == null) {
for (Node<E> x = last; x != null; x = x.prev) {
if (x.item == null) {
unlink(x);
return true;
}
}
} else {
for (Node<E> x = last; x != null; x = x.prev) {
if (o.equals(x.item)) {
unlink(x);
return true;
}
}
}
return false;
} //返回一个列表迭代器,可以向前或向后迭代,index指定起始位置
public ListIterator<E> listIterator(int index) {
checkPositionIndex(index);
return new ListItr(index);
} //返回从链尾开始的迭代器,在ListIterator的基础上实现的
public Iterator<E> descendingIterator() {
return new DescendingIterator();
}

支持clone

public Object clone() {
LinkedList<E> clone = superClone(); // Put clone into "virgin" state
clone.first = clone.last = null;
clone.size = 0;
clone.modCount = 0; // Initialize clone with our elements
for (Node<E> x = first; x != null; x = x.next)
clone.add(x.item); return clone;
}

支持序列化和反序列化

private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException {
// Write out any hidden serialization magic
s.defaultWriteObject(); // Write out size
s.writeInt(size); // Write out all elements in the proper order.
for (Node<E> x = first; x != null; x = x.next)
s.writeObject(x.item);
} private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
// Read in any hidden serialization magic
s.defaultReadObject(); // Read in size
int size = s.readInt(); // Read in all elements in the proper order.
for (int i = 0; i < size; i++)
linkLast((E)s.readObject());
}

java.util.LinkedList源码分析的更多相关文章

  1. java.util.Collection源码分析和深度讲解

    写在开头 java.util.Collection 作为Java开发最常用的接口之一,我们经常使用,今天我带大家一起研究一下Collection接口,希望对大家以后的编程以及系统设计能有所帮助,本文所 ...

  2. java.util.HashMap源码分析

    在java jdk8中对HashMap的源码进行了优化,在jdk7中,HashMap处理“碰撞”的时候,都是采用链表来存储,当碰撞的结点很多时,查询时间是O(n). 在jdk8中,HashMap处理“ ...

  3. java.util.AbstractStringBuilder源码分析

    AbstractStringBuilder是一个抽象类,是StringBuilder和StringBuffer的父类,分析它的源码对StringBuilder和StringBuffer代码的理解有很大 ...

  4. java.util.Hashtable源码分析

    Hashtable实现一个键值映射的表.任何非null的object可以用作key和value. 为了能存取对象,放在表里的对象必须实现hashCode和equals方法. 一个Hashtable有两 ...

  5. java.util.Dictionary源码分析

    Dictionary是一个抽象类,Hashtable是它的一个子类. 类的声明:/** The <code>Dictionary</code> class is the abs ...

  6. java中LinkedList源码分析

    ArrayList是动态数组,其实本质就是对数组的操作.那么LinkedList实现原理和ArrayList是完全不一样的.现在就来分析一下ArrayList和LinkeList的优劣吧LinkedL ...

  7. Java集合-LinkedList源码分析

    目录 1.数据结构-链表 2.ArrayList结构特性 3.构造方法 4.成员变量 5.常用的成员方法 6.Node节点 7.序列化原理 8.迭代器 9.总结 1.数据结构-链表 链表(Linked ...

  8. java.util.TreeSet源码分析

    TreeSet是基于TreeMap实现的,元素的顺序取决于元素自身的自然顺序或者在构造时提供的比较器. 对于add,remove,contains操作,保证log(n)的时间复杂度. 因为Set接口的 ...

  9. java.util.TreeMap源码分析

    TreeMap的实现基于红黑树,排列的顺序根据key的大小,或者在创建时提供的比较器,取决于使用哪个构造器. 对于,containsKey,get,put,remove操作,保证时间复杂度为log(n ...

随机推荐

  1. android Camera 数据流程分析

    这篇文章主要针对其数据流程进行分析.Camera一般用于图像浏览.拍照和视频录制.这里先对图像浏览和拍照的数据流进行分析,后面再对视频电话部分进行分析. 1.针对HAL层对摄像头数据处理补充一下 Li ...

  2. ipad mini2 ios7 磁盘分析文件夹大小

    如果没越狱可以通过, 设置->通用->用量   来查看磁盘占用 越狱之后, 莫名其妙地 "其他" 占了两三个G.. windows下有个好用的工具folder size ...

  3. C# 之 Stream 和 byte[] 的相关转换

    1.二进制转换为图片 MemoryStream ms = new MemoryStream(bytes); ms.Position = ; Image img = Image.FromStream(m ...

  4. B. Little Dima and Equation

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  5. AJAX XMLHttpRequest

    <html> <head> <title>XMLHTTPRequest对象的说明DEMO</title> <script language=&qu ...

  6. iOS-实现验证码倒计时功能(1)

    验证码倒计时按钮的应用是非常普遍的,该Blog就和你一起来写一个IDCountDownButton来实现验证码倒计时的效果.你可以想使用普通的UIButton类型按钮一样,只需要设置其倒计时时长(若未 ...

  7. ES各种错误解决

    _update API 执行报错 错误信息: { "error": { "root_cause": [ { "type": "re ...

  8. [设计模式]<<设计模式之禅>>抽象工厂模式

    1 女娲的失误 上一篇讲了女娲造人的故事.人是造出来了,世界也热闹了,可是低头一看,都是清一色的类型,缺少关爱.仇恨.喜怒哀乐等情绪,人类的生命太平淡了,女娲一想,猛然一拍 脑袋,忘记给人类定义性别了 ...

  9. 关于Eclipse插件之IWorkbench IWorkbenchWindow IWorkbenchPage |WorkbenchPart......等的总结

    1..IWorkbench: workbench是eclipse用户界面中最底层的对象,它建立在Display之上,包含一个或多个IWorkbenchWindow,用于向终端用户呈现信息 当你的wor ...

  10. JavaScript基础笔记二

    一.函数返回值1.什么是函数返回值    函数的执行结果2. 可以没有return // 没有return或者return后面为空则会返回undefined3.一个函数应该只返回一种类型的值 二.可变 ...