java.util.LinkedList源码分析
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源码分析的更多相关文章
- java.util.Collection源码分析和深度讲解
写在开头 java.util.Collection 作为Java开发最常用的接口之一,我们经常使用,今天我带大家一起研究一下Collection接口,希望对大家以后的编程以及系统设计能有所帮助,本文所 ...
- java.util.HashMap源码分析
在java jdk8中对HashMap的源码进行了优化,在jdk7中,HashMap处理“碰撞”的时候,都是采用链表来存储,当碰撞的结点很多时,查询时间是O(n). 在jdk8中,HashMap处理“ ...
- java.util.AbstractStringBuilder源码分析
AbstractStringBuilder是一个抽象类,是StringBuilder和StringBuffer的父类,分析它的源码对StringBuilder和StringBuffer代码的理解有很大 ...
- java.util.Hashtable源码分析
Hashtable实现一个键值映射的表.任何非null的object可以用作key和value. 为了能存取对象,放在表里的对象必须实现hashCode和equals方法. 一个Hashtable有两 ...
- java.util.Dictionary源码分析
Dictionary是一个抽象类,Hashtable是它的一个子类. 类的声明:/** The <code>Dictionary</code> class is the abs ...
- java中LinkedList源码分析
ArrayList是动态数组,其实本质就是对数组的操作.那么LinkedList实现原理和ArrayList是完全不一样的.现在就来分析一下ArrayList和LinkeList的优劣吧LinkedL ...
- Java集合-LinkedList源码分析
目录 1.数据结构-链表 2.ArrayList结构特性 3.构造方法 4.成员变量 5.常用的成员方法 6.Node节点 7.序列化原理 8.迭代器 9.总结 1.数据结构-链表 链表(Linked ...
- java.util.TreeSet源码分析
TreeSet是基于TreeMap实现的,元素的顺序取决于元素自身的自然顺序或者在构造时提供的比较器. 对于add,remove,contains操作,保证log(n)的时间复杂度. 因为Set接口的 ...
- java.util.TreeMap源码分析
TreeMap的实现基于红黑树,排列的顺序根据key的大小,或者在创建时提供的比较器,取决于使用哪个构造器. 对于,containsKey,get,put,remove操作,保证时间复杂度为log(n ...
随机推荐
- android短信发送器源代码
Activity类: import java.util.List;import android.app.Activity;import android.app.PendingIntent;import ...
- C++编译器默默编写并调用哪些函数
什么时候empty class(空类)不再是个empty class呢?当C++处理过它之后,是的,如果你自己没有声明,编译器就会为它声明(编译器版本)一个copy构造函数.一个copy assign ...
- How to update FVDI Commander driver to latest V2015.6.2
As FVDI Commander products are upgraded to new versions, I often receive emails from customers askin ...
- 字体的大小(pt)和像素(px)如何转换?
px:相对长度单位.像素(Pixel). pt:绝对长度单位.点(Point). 1in = 2.54cm = 25.4 mm = 72pt = 6pc 具体换算是: Points Pixels Em ...
- angular 项目回顾
从学习angular,到实际项目开发不到一周,完全是边写边学呀,都是为了项目,已使用angular 开发了两个项目了,有些技术当时只是会用,都没好好回顾一下,现在有时间回顾一下,项目中用到的一些指令, ...
- C#中,为什么结构体也可以设置构造函数?
结构体派生自ValueType,ValueType派生自Object,可访问Object的方法.结构体是一种缩小版的类.结构体不能继承.结构体总是有一个无参数的默认构造函数,不允许替换.结构体可指定字 ...
- python(2)-字符串(2)
字符串格式化: 前面说过一种字符串格式化方法,来复习一下: >>> print('His name is %s', 'jeff') His name is %s jeff 其实格式化 ...
- 使用copy再次实现Circle类,保证不能有内存泄漏问题
#import <Foundation/Foundation.h> //xieyi @protocol showOn @required -(void)printOn; @end // l ...
- UE设置 去掉bak备份文件
使用ue打开文件,修改保存后,会产生.bak备份文件,感觉不爽,如何去掉呢? 1:在ue菜单栏,选择“高级”按钮选项 —— “配置”选项 2:在弹出的选择框中,找到“备份”—— 勾选“不备份” 选项 ...
- [转]Maintain File Upload Control on Postbacks
本文转自:http://www.ironspeed.com/articles/Maintain%20File%20Upload%20Control/Article.aspx Introduction ...