简介
LinkedList是一个实现了List接口和Deque接口的双端链表。因为它的链表结构使他具备了快速插入和删除的特性、以及它实现了Deque接口使他具备了队列的特性。Linkedlist是线程不安全的,所以要想让他具备线程安全可以使用Connections类的方法:
List list=Collections.synchronizedList(new LinkedList(...));
内部结构分析:
这里可以看出链表结构其实是由一个个的Node连接组成的,
这里的内部类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;
}
}
LinkedList源码分析
1、首先下手构造方法:
~无参:
public LinkedList(){
}
有参:已有集合作为参数
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
add方法:
1.1:添加到链表尾部
public boolean add(E e) {
linkLast(e);//这里就只调用了这一个方法
return true;
}
/**
* 链接使e作为最后一个元素。
*/
void linkLast(E e) {
final Node<E> l = last;// last指针指向l节点
final Node<E> newNode = new Node<>(l, e, null);//创建新节点,前驱节点是l,值为e,后驱节点是null
last = newNode;//让新节点为最后指针对应
if (l == null)//如果没有前驱指针说明为第一个节点
first = newNode;
else
l.next = newNode;//指向后继元素也就是指向下一个元素
size++;
modCount++;
}
1.2:add(int index,E e):在指定位置添加元素
public void add(int index, E element) {
checkPositionIndex(index); //检查索引是否处于[0-size]之间
if (index == size)//添加在链表尾部
linkLast(element);
else//添加在链表中间
linkBefore(element, node(index));
}
linkBefore方法需要给定两个参数,一个插入节点的值,一个指定的node,所以我们又调用了Node(index)去找到index对应的node
addAll(Collection c ):将集合插入到链表尾部
public boolean addAll(Collection<? extends E> c) {
return addAll(size, c);//加到原有链表尾部
}
addAll(int index, Collection c): 将集合从指定位置开始插入
public boolean addAll(int index, Collection<? extends E> c) {
//1:检查index范围是否在size之内
checkPositionIndex(index);
//2:toArray()方法把集合的数据存到对象数组中
Object[] a = c.toArray();
int numNew = a.length;
if (numNew == 0)//这里就是为空说明集合没有元素
return false;
//3:得到插入位置的前驱节点和后继节点
Node<E> pred, succ;
//如果插入位置为尾部,前驱节点为last,后继节点为null
if (index == size) {
succ = null;
pred = last;
}
//否则,调用node()方法得到后继节点,再得到前驱节点
else {
succ = node(index);
pred = succ.prev;
}
// 4:遍历数据将数据插入
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;
}
//如果插入位置在尾部,重置last节点
if (succ == null) {
last = pred;
}
//否则,将插入的链表与先前链表连接起来
else {
pred.next = succ;
succ.prev = pred;
}
size += numNew;
modCount++;
return true;
}
上面可以看出addAll方法通常包括下面四个步骤:
1、检查index是否在size内。
2、toArray()方法把集合的数据存到对象数组中
3、得到插入位置的前驱和后继节点
4、遍历数据,将数据插入到指定位置
addFirst(E e): 将元素添加到链表头部
public void addFirst(E e) {
linkFirst(e);
}
private void linkFirst(E e) {
final Node<E> f = first;//先获取到第一个节点
final Node<E> newNode = new Node<>(null, e, f);//新建节点,以头节点为后继节点
first = newNode;
//如果链表为空,last节点也指向该节点
if (f == null)
last = newNode;
//否则,将头节点的前驱指针指向新节点,也就是指向前一个元素
else
f.prev = newNode;
size++;
modCount++;
}
addLast(E e): 将元素添加到链表尾部,与 add(E e) 方法一样
根据位置取数据的方法
get(int index): 根据指定索引返回数据
public E get(int index) {
//检查index范围是否在size之内
checkElementIndex(index);
//调用Node(index)去找到index对应的node然后返回它的值
return node(index).item;
}
获取头节点(index=0)数据方法
public E getFirst() {
final Node<E> f = first;//定义第一个节点
if (f == null)
throw new NoSuchElementException();//第一个节点没有就抛出异常
return f.item;//返回第一个节点的值
}
public E element() {
return getFirst();
}
public E peek() {
final Node<E> f = first;
return (f == null) ? null : f.item;
}
public E peekFirst() {
final Node<E> f = first;
return (f == null) ? null : f.item;
}
区别:
getFirst() 、element()、peek()、peekFirst()这几个方法中主要区别,在于如果节点为null时的处理方式、getFirst和element方法是抛出异常,peek和peekFirst是返回为null。
获取为节点index(-1)的方法
public E getLast() {
final Node<E> l = last;//获取当前尾节点
if (l == null)
throw new NoSuchElementException();
return l.item;//得到当前节点数据
}
public E peekLast() {
final Node<E> l = last;
return (l == null) ? null : l.item;
}
区别:
getLast方法在最后节点为空时会抛出异常、peekLast不会
根据对象得到索引的方法
int indexOf(Object o): 从头遍历找
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;
}
int lastIndexOf(Object o): 从尾遍历找
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;
}
检查链表是否包含某对象的方法:
contains(Object o): 检查对象o是否存在于链表中
public boolean contains(Object o) {
return indexOf(o) != -1;
}
删除方法
remove() ,removeFirst(),pop(): 删除头节点
public E pop() {
return removeFirst();
}
public E remove() {
return removeFirst();
}
public E removeFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return unlinkFirst(f);
}
这里可以看出来都是remove 、pop 在调用removeFirst方法实现
removeLast(),pollLast(): 删除尾节点
public E removeLast() {
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
return unlinkLast(l);
}
public E pollLast() {
final Node<E> l = last;
return (l == null) ? null : unlinkLast(l);
}
大致区别都是大同小异。
remove(Object o): 删除指定元素
public boolean remove(Object o) {
//如果删除对象为null
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;
}