collection 源码
转自:http://blog.csdn.net/longlong2015/article/details/48174421
http://blog.csdn.net/mra__s__/article/details/55517204
http://www.cnblogs.com/chenssy/p/3746600.html
JDK 1.7源码阅读笔记(三)集合类之LinkedList
标签: jdklinkedlist源码阅读 2015-09-02 09:58 644人阅读 评论(0) 收藏 举报 分类: JDK源码(6)版权声明:本文为博主原创文章,未经博主允许不得转载。
目录(?)[+]
前言
(1)LinkedList的内部实现是双向链表,继承了AbstractSequentialList,实现了List, Deque, Cloneable, Java.io.Serializable接口,因此LinkdeList本身支持就支持双端队列操作。LinkedList**允许所有元素(包括 null)**。除了实现 List 接口外,LinkedList 类还为在列表的开头及结尾 get、remove 和 insert 元素提供了统一的命名方法。这些操作允许将链接列表用作堆栈、队列或双端队列。
此类实现 Deque 接口,为 add、poll 提供先进先出队列操作,以及其他堆栈和双端队列操作。
LinkedList与ArrayList一样实现List接口,只是ArrayList是List接口的大小可变数组的实现,LinkedList是List接口链表的实现。基于链表实现的方式使得LinkedList在插入和删除时更优于ArrayList,而随机访问则比ArrayList逊色些。
(2)此实现不是同步的。如果多个线程同时访问一个链接列表,而其中至少一个线程从结构上修改了该列表,则它必须 保持外部同步。(结构修改指添加或删除一个或多个元素的任何操作;仅设置元素的值不是结构修改。)这一般通过对自然封装该列表的对象进行同步操作来完成。如果不存在这样的对象,则应该使用 Collections.synchronizedList 方法来“包装”该列表。最好在创建时完成这一操作,以防止对列表进行意外的不同步访问,如下所示:
List list = Collections.synchronizedList(new LinkedList(...));(3) 此类的 iterator 和 listIterator 方法返回的迭代器是快速失败 的:在迭代器创建之后,如果从结构上对列表进行修改,除非通过迭代器自身的 remove 或 add 方法,其他任何时间任何方式的修改,迭代器都将抛出 ConcurrentModificationException。因此,面对并发的修改,迭代器很快就会完全失败,而不冒将来不确定的时间任意发生不确定行为的风险。
注意,迭代器的快速失败行为不能得到保证,一般来说,存在不同步的并发修改时,不可能作出任何硬性保证。快速失败迭代器尽最大努力抛出 ConcurrentModificationException。因此,编写依赖于此异常的程序的方式是错误的,正确做法是:迭代器的快速失败行为应该仅用于检测程序错误。
(4)和ArrayList类似,在类内部同样出现了transient关键字,这在ArrayList中已经解释过了,在这里不做过多的解释了。
transient int size = 0;transient Node<E> first;transient Node<E> last;- 1
- 2
- 3
- 1
- 2
- 3
源码
1>LinkedList内部通过Node来抽象一个节点,结点包括值和前向指针和后向指针。Node的定义是在LinkedList内部,作为静态内部类存在。
private static class Node<E> { E item;//结点的值 Node<E> next;//结点的后向指针 Node<E> prev;//结点的前向指针 //构造函数中已完成Node成员的赋值 Node(Node<E> prev, E element, Node<E> next) { this.item = element;//结点的值赋值为element this.next = next;//后向指针赋值 this.prev = prev;//前向指针赋值 } }- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
2>LinkedList 的关键源码
//LinedList继承了AbstractSequentialList,支持泛型 public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable, java.io.Serializable { transient int size = 0;//链表元素个数 transient Node<E> first;//链表头结点 transient Node<E> last;//链表尾结点 //生成一个空的链表 public LinkedList() { } //按c里面的元素生成一个LinkedList public LinkedList(Collection<? extends E> c) { this();//调用空的构造函数 addAll(c);//将c里面的元素添加到空链表尾部 } //首部增加结点,结点的值为e private void linkFirst(E e) { final Node<E> f = first;//f指向头结点 //生成一个新结点,结点的值为e,其前向指针为null,后向指针为f final Node<E> newNode = new Node<>(null, e, f); //first指向新生成的结点,f保存着老的头结点信息 first = newNode; if (f == null) //如果f为null,则表示整个链表目前是空的,则尾结点也指向新结点 last = newNode; else //f(老的头结点)的前向指针指向最新的结点信息 f.prev = newNode; size++;//元素个数+1. modCount++;//修改次数+1 } //尾部增加结点,结点的值为e void linkLast(E e) { final Node<E> l = last;//l指向尾结点 //生成一个新结点,结点的值为e,其前向指针为l,后向指针为null final Node<E> newNode = new Node<>(l, e, null); //last指向新生成的结点,l保存着老的尾结点信息 last = newNode; if (l == null) //如果l为null,则表示整个链表目前是空的,则头结点也指向新结点 first = newNode; else //l(老的尾结点)的后向指针指向最新的结点信息 l.next = newNode; size++;//元素个数+1 modCount++;//修改次数+1 } //非空结点succ之前插入新结点,新结点的值为e void linkBefore(E e, Node<E> succ) { // assert succ != null;//外界调用需保证succ不为null,否则程序会抛出空指针异常 final Node<E> pred = succ.prev;//pred指向succ的前向结点 //生成一个新结点,结点的值为e,其前向指针指向pred,后向指针指向succ final Node<E> newNode = new Node<>(pred, e, succ); succ.prev = newNode;//succ的前向指针指向newNode if (pred == null) //如果pred为null,则表示succ为头结点,此时头结点指向最新生成的结点newNode first = newNode; else //pred的后向指针指向新生成的结点,此时已经完成了结点的插入操作 pred.next = newNode; size++;//元素个数+1 modCount++;//修改次数+1 } //删除头结点,并返回该结点的值 private E unlinkFirst(Node<E> f) { // assert f == first && f != null;//需确保f为头结点,且链表不为Null final E element = f.item;//获得结点的值 final Node<E> next = f.next;//next指向f的后向结点 f.item = null;//释放数据结点 f.next = null;//释放f的后向指针 first = next;//first指向f的后向结点 if (next == null) //如果next为null,则表示f为last结点,此时链表即为空链表 last = null; else //修改next的前向指针,因为first结点的前向指针为null next.prev = null; size--;//元素个数-1 modCount++;//修改次数+1 return element; } //删除尾结点,并返回尾结点的内容 private E unlinkLast(Node<E> l) { // assert l == last && l != null;//需确保l为尾结点,且链表不为null final E element = l.item;//获得结点的值 final Node<E> prev = l.prev;//prev执行l的前向结点 l.item = null;//释放l结点的值 l.prev = null; //释放l结点的前向指针 last = prev;//last结点指向l的前向结点 if (prev == null) //如果prev为null,则表示l为first结点,此时链表即为空链表 first = null; else //修改prev的后向指针,因为last结点的后向指针为null prev.next = null; size--;//元素个数-1 modCount++;//修改次数+1 return element; } //删除结点x E unlink(Node<E> x) { // assert x != null;//需确保x不为null,否则后续操作会抛出空指针异常 final E element = x.item;//保存x结点的值 final Node<E> next = x.next;//next指向x的后向结点 final Node<E> prev = x.prev;//prev指向x的前向结点 if (prev == null) { //如果prev为空,则x结点为first结点,此时first结点指向next结点(x的后向结点) first = next; } else { prev.next = next;//x的前向结点的后向指针指向x的后向结点 x.prev = null;//释放x的前向指针 } if (next == null) { //如果next结点为空,则x结点为尾部结点,此时last结点指向prev结点(x的前向结点) last = prev; } else { next.prev = prev;//x的后向结点的前向指针指向x的前向结点 x.next = null;//释放x的后向指针 } x.item = null;//释放x的值节点,此时x节点可以完全被GC回收 size--;//元素个数-1 modCount++;//修改次数+1 return element; } //获得头结点的值 public E getFirst() { final Node<E> f = first;//f指向first结点 if (f == null)//此时链表为空 throw new NoSuchElementException(); return f.item;//返回first结点的值 } //获得尾结点的值 public E getLast() { final Node<E> l = last;//l指向last结点 if (l == null)//此时链表为空 throw new NoSuchElementException(); return l.item;//返回last结点的值 } //移除头结点 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);//摘除尾结点 } //添加到头结点,结点的值为e public void addFirst(E e) { linkFirst(e);//添加到头部 } //添加到尾结点,结点值为e public void addLast(E e) { linkLast(e);//添加到尾部 } //判断元素(值为o)是o否在链表中 public boolean contains(Object o) { return indexOf(o) != -1;//定位元素 } //返回元素个数 public int size() { return size; } //添加元素,元素值为e public boolean add(E e) { linkLast(e);//添加到链表尾部 return true; } //移除值为o的元素,o可以为null,找到一个删除即返回 public boolean remove(Object o) { if (o == null) {//元素为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中的元素都添加到当前链表中 public boolean addAll(Collection<? extends E> c) { return addAll(size, c);//添加到链表尾部 } //第序号为index处,添加c中所有的元素到当前链表中(后向添加的) 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<E> pred, succ; if (index == size) {//如果index为元素个数,即第index个结点为尾结点 succ = null; pred = last;//指向为结点 }转载于:https://www.cnblogs.com/thxu/p/7406971.html
总结
以上是生活随笔为你收集整理的collection 源码的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: HTTP 错误 404.15 - Not
- 下一篇: 项目管理三大认证体系,该选择谁?