java集合之家——LinkList(源码)

前面分析了ArrayList,可知它的底层实现就是一个普通数组而已,用到的数据结构就是顺序表。虽然说对于随机访问效率是还可以的,但是如果要进行插入删除的的话,因为要移动大批的元素,导致性能非常地差,所以我们不得不研究出另一种数据结构,可以高效率的实现插入删除,这个数据结构就是链表。而今天的LinkList就利用了双向循环链表实现了这个数据结构。下面我们就来看看源码。

类声明:
public class LinkedList
extends AbstractSequentialList
implements List, Deque, Cloneable, java.io.Serializable
继承了AbstractSequentialList,实现了List,Deque(这个就是一个双端队列),Cloneable。java.io.Serializable。
这里就只说一下AbstractSequentialList,Deque以后另写。

AbstractSequentialList
类声明:
public abstract class AbstractSequentialList extends AbstractList {
继承了AbstractList,ArrayList的父类也有类似的继承关系
构造方法:
protected AbstractSequentialList() { }
获取index元素
public E get(int index) {
try {
return listIterator(index).next();//调用lslitlterator
} catch (NoSuchElementException exc) {
throw new IndexOutOfBoundsException("Index: "+index);
}
}
public abstract ListIterator listIterator(int index);//这个方法由子类实现
设置元素:
public E set(int index, E element) {
try {
ListIterator e = listIterator(index);//调用listlterator方法
E oldVal = e.next();
e.set(element);
return oldVal;
} catch (NoSuchElementException exc) {
throw new IndexOutOfBoundsException("Index: "+index);
}
}
添加元素:
public void add(int index, E element) {
try {
listIterator(index).add(element);
} catch (NoSuchElementException exc) {
throw new IndexOutOfBoundsException("Index: "+index);
}
}
删除元素:
public E remove(int index) {
try {
ListIterator e = listIterator(index);
E outCast = e.next();
e.remove();
return outCast;
} catch (NoSuchElementException exc) {
throw new IndexOutOfBoundsException("Index: "+index);
}
}
public boolean addAll(int index, Collection<? extends E> c) {
try {
boolean modified = false;
ListIterator e1 = listIterator(index);
Iterator<? extends E> e2 = c.iterator();
while (e2.hasNext()) {
e1.add(e2.next());
modified = true;
}
return modified;
} catch (NoSuchElementException exc) {
throw new IndexOutOfBoundsException("Index: "+index);
}
}
public Iterator iterator() {
return listIterator();
}
其实上面这些方法在子类LinkList都有类似的实现。


LinkList源码


变量:
transient int size = 0;//这个linklist的大小
transient Node first;//头结点,永远指向第一个元素
transient Node last;//尾节点,永远指向最后一个元素
构造方法:
public LinkedList() { }无参构造方法
public LinkedList(Collection<? extends E> c) {将集合变成一个双向链表linklist
this();
addAll©;
}
————————————————————————————————————————
将元素插入到第一个结点前面
private void linkFirst(E e) {
final Node f = first;将头结点赋给f,也就是说f指向了第一个结点
final Node newNode = new Node<>(null, e, f);建立一个新的newNode,这里的Node是后面结点的包装,这个新结点就是传过来的e
first = newNode;将first指向这个新节点
if (f == null)如果f指向的第一个结点为空,也就是这个链表为空的时候就应该让头结点尾节点都只想newNode
last = newNode;
else
f.prev = newNode;否则第一个结点不是空,这样就将newNode插入到第一个结点的前面
size++;长度+1

modCount++;//修改次数+1
}
—————————————————————————————————————————
void linkLast(E e) :和linkfirst差不多,就是插入到尾节点
添加节点方法:默认添加到尾部,代用linkLast
public boolean add(E e) {
linkLast(e);
return true;
}
—————————————————————————————————————————
将e插入到succ前面:
void linkBefore(E e, Node succ) {
final Node pred = succ.prev;//将succ结点的前驱赋给pred
final Node newNode = new Node<>(pred, e, succ);//根据e和succ得到一个新节点
succ.prev = newNode;//succ.prev就是succ的前驱就是这个新节点
if (pred == null)//如果前驱为空说明succ是第一个结点。相当于要插入到第一个结点,也就是实现linkfirst
first = newNode;
else
pred.next = newNode;//不是空说明就是中间的点,这样直接让succ的前驱结点的后继等于newNode即可,而pred,newNode,succ的其他关系都在Node里面实现了
size++;
modCount++;
}
—————————————————————————————————————————
删除第一个元素:
private E unlinkFirst(Node f) {
final E element = f.item;
final Node next = f.next;
f.item = null;
f.next = null; // 依靠GC回收
first = next;//指向第一个结点的下一个,因为第一个结点已经删除了。
if (next == null)//如果这个节点没有后继,说明整个表只有一个节点,那么last也只能为空
last = null;
else
next.prev = null;
size–;
modCount++;
return element;
}
private E unlinkLast(Node l)同理,删除出最后一个元素
—————————————————————————————————————————
删除某一个元素x:
E unlink(Node x) {
final E element = x.item;
final Node next = x.next;/
/先取出x的下一个元素

final Node prev = x.prev;
//先取出x的上一个元素
*
if (prev == null) {//前驱为空说明是第一个元素,另first指向第一个元素后继就可以了
first = next;
} else {//前驱不是空,就是让prev的后继指向删除元素x的后继next
prev.next = next;
x.prev = null;
}
if (next == null) {//如果next为空说明x是最后一个元素,另last指向x的前驱就可以了。
last = prev;
} else {
next.prev = prev;//后继不是空,就让后继的前驱指向删除元素x的前驱prev
x.next = null;
}
x.item = null;
size–;
modCount++;
return element;
}
供对象调用的删除方法:
public boolean remove(Object o) {
if (o == null) {
for (Node x = first; x != null; x = x.next) {//遍历聊表找到null
if (x.item == null) {
unlink(x);//调用上面的unlink删除元素
return true;
}
}
} else {
for (Node x = first; x != null; x = x.next) {//遍历链表找到对象o
if (o.equals(x.item)) {
unlink(x);//
return true;
}
}
}
return false;
}
—————————————————————————————————————————
获取第一个元素
public E getFirst() {
final Node f = first;
if (f == null)
throw new NoSuchElementException();
return f.item;
}
获取最后一个元素
public E getLast() {
final Node l = last;
if (l == null)
throw new NoSuchElementException();
return l.item;
}
—————————————————————————————————————————
删除第一个元素,调用unlinkFirst实现
public E removeFirst() {
final Node f = first;
if (f == null)
throw new NoSuchElementException();
return unlinkFirst(f);
}
—————————————————————————————————————————
删除最后一个元素,调用unlinkLast实现
public E removeLast() {
final Node 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;调用下面的indexof
}
indexof方法:
public int indexOf(Object o) {
int index = 0;
if (o == null) {
for (Node x = first; x != null; x = x.next) {//从前往后遍历,找到第一个空的
if (x.item == null)
return index;
index++;
}
} else {
for (Node x = first; x != null; x = x.next) {//从前往后遍历,找到第一个o
if (o.equals(x.item))
return index;
index++;
}
}
return -1;
}—————————————————————————————————————————
返回链表大小
public int size() {
return size;
}
—————————————————————————————————————————
向链表中添加一个集合中的元素:
public boolean addAll(Collection<? extends E> c) {
return addAll(size, c);//调用下面的addAll
}
真正的添加方法:
public boolean addAll(int index, Collection<? extends E> c) {
checkPositionIndex(index);//检查index是否越界
Object[] a = c.toArray();//将集合转换成一个数组
int numNew = a.length;
if (numNew == 0)
return false;
Node pred, succ;
if (index == size) {//如果要插入的位置是最后面,那么前驱就是last节点,后继暂时为空
succ = null;
pred = last;
} else {
succ = node(index);//否则后继就是第index个节点
pred = succ.prev;//要插入的点的前驱
}
for (Object o : a) {
@SuppressWarnings(“unchecked”) E e = (E) o;
Node 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 x = first; x != null; ) {
Node 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;//调用node方法
}
node方法:这里采用了索引分开搜索的方法,缩小一半的搜寻量
Node node(int index) {
if (index < (size >> 1)) {//如果索引index小于size的一半
Node x = first;
for (int i = 0; i < index; i++)
x = x.next;
return x;
} else {//如果索引index大于size的一半
Node x = last;
for (int i = size - 1; i > index; i–)
x = x.prev;
return x;
}
}
—————————————————————————————————————————
从后往前索引
public int lastIndexOf(Object o) {
int index = size;
if (o == null) {
for (Node x = last; x != null; x = x.prev) {
index–;
if (x.item == null)
return index;
}
} else {
for (Node x = last; x != null; x = x.prev) {
index–;
if (o.equals(x.item))
return index;
}
}
return -1;
}
—————————————————————————————————————————
栈模拟操作
public void push(E e) {入栈
addFirst(e);
}
public E pop() {出栈
return removeFirst();
} —————————————————————————————————————————
返回一个迭代器
public ListIterator listIterator(int index) {
checkPositionIndex(index);//检查是否越界
return new ListItr(index);/
/直接new一个Listltr对象,就是下面这个类**
}
—————————————————
private class ListItr implements ListIterator {
private Node lastReturned;
private Node next;
private int nextIndex;
private int expectedModCount = modCount;
ListItr(int index) {
next = (index == size) ? null : node(index);//如果索引是最后一个,那么就是null的迭代了,否则就返回索引的那个元素,以他为迭代器的首位元素。
nextIndex = index;
}
public boolean hasNext() {//是否有下一个元素
return nextIndex < size;
}
public E next() {//获取下一个元素
checkForComodification();//检查当前操作是否一样,一面线程不安全
if (!hasNext())
throw new NoSuchElementException();
lastReturned = next;
next = next.next;
nextIndex++;
return lastReturned.item;
}
public boolean hasPrevious() {
return nextIndex > 0;
}
public E previous() {//获取前一个元素
checkForComodification();
if (!hasPrevious())
throw new NoSuchElementException();
lastReturned = next = (next == null) ? last : next.prev;
nextIndex–;
return lastReturned.item;
}
public int nextIndex() {
return nextIndex;
}
public int previousIndex() {
return nextIndex - 1;
}
public void remove() {//移除
checkForComodification();
if (lastReturned == null)
throw new IllegalStateException();
Node lastNext = lastReturned.next;
unlink(lastReturned);
if (next == lastReturned)
next = lastNext;
else
nextIndex–;
lastReturned = null;
expectedModCount++;
}
public void set(E e) {
if (lastReturned == null)
throw new IllegalStateException();
checkForComodification();
lastReturned.item = e;
}
public void add(E e) {
checkForComodification();
lastReturned = null;
if (next == null)
linkLast(e);
else
linkBefore(e, next);
nextIndex++;
expectedModCount++;
}
public void forEachRemaining(Consumer<? super E> action) {
Objects.requireNonNull(action);
while (modCount == expectedModCount && nextIndex < size) {
action.accept(next.item);
lastReturned = next;
next = next.next;
nextIndex++;
}
checkForComodification();
}
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}
—————————————————————————————————————————
节点包装类
private static class Node {
E item;
Node next;//后继
Node prev;//前驱
Node(Node prev, E element, Node next) {
this.item = element;
this.next = next;
this.prev = prev;
}
}
—————————————————————————————————————————
还有一些方法我就没有列举了,基本和ArrayList差不多,还有就是调用父类的方法,然后实现了克隆,序列化等等,不是主要的我就不仔细介绍了。
—————————————————————————————————————————


总结


和ArrayList比较起来,LinkList更适用于插入删除,它作为双向链表来实现的。
所以插入和删除操作明显效率要高于ArrayList,索引查找的话虽然不如ArrayList,但是也提供了索引减半的优化了,缩小了一般的搜寻量。和ArrayList一样,他也是线程不安全的,容易被多个线程修改访问,尽量要避免这种情况,采用同步或者锁的方法。
好了这篇文章就写到这里了。

posted @ 2019-03-31 21:30  ongbo  阅读(38)  评论(0)    收藏  举报