参考:http://www.cnblogs.com/skywang12345/p/3308807.html

Consumer.class   消费者接口

参考:https://www.jianshu.com/p/63771441ba31

https://blog.csdn.net/qq_28410283/article/details/80618456

  1. public class ConsumerTest {
  2. public static void main(String[] args) {
  3. //testConsumer();
  4. testAndThen();
  5. }
  6.  
  7. public static void testConsumer(){
  8. Consumer<Integer> square=x->System.out.println("print square: "+x*x);
  9. square.accept(2);
  10. }
  11.  
  12. public static void testAndThen() {
  13. Consumer<Integer> consumer1=x->System.out.println("first x :"+x);
  14. Consumer<Integer> consumer2=x->{
  15. System.out.println("second x: "+x);
  16. throw new NullPointerException("throw exception test");
  17. };
  18. Consumer<Integer> consumer3=x->System.out.println("third x: "+x);
  19. consumer1.andThen(consumer2).andThen(consumer3).accept(1);
  20. }
  21. }

函数式编程与lambda表达式:

https://www.cnblogs.com/snowInPluto/p/5981400.html

https://www.cnblogs.com/Dorae/p/7769868.html

https://www.cnblogs.com/CarpenterLee/p/6729368.html

LinkList.class       public boolean addAll(int index, Collection<? extends E> c)

  1. public boolean addAll(int index, Collection<? extends E> c) { 将一个集合c插入到链表中,index的位置
  2. checkPositionIndex(index);
  3. Object[] a = c.toArray();
  4. int numNew = a.length;
  5. if (numNew == 0)
  6. return false;
  7.  
  8. Node<E> pred, succ;
  9. if (index == size) { 若是插入到最后一个元素的后面
  10. succ = null; 这个元素指向null
  11. pred = last; 这个元素的前向指针指向之前的最后一个元素
  12. } else {
  13. succ = node(index); 这个元素后向指针指向原来index处的元素
  14. pred = succ.prev; 这个元素前向指针指向原来index处元素的前一个元素
  15. }
  16.  
  17. for (Object o : a) { 将这些元素挨个的插入进链表
  18. @SuppressWarnings("unchecked") E e = (E) o;
  19. Node<E> newNode = new Node<>(pred, e, null); 将插元素的前向指针指向前一个元素
  20. if (pred == null)
  21. first = newNode;
  22. else
  23. pred.next = newNode; 将前一个元素的后向指针指向当前插入的元素
  24. pred = newNode; 往后移一位
  25. }
  26.  
  27. if (succ == null) {
  28. last = pred;
  29. } else {
  30. pred.next = succ;
  31. succ.prev = pred;
  32. }
  33.  
  34. size += numNew;
  35. modCount++;
  36. return true;
  37. }
  1. Node<E> node(int index) { 这里采用二分法去查找 链表上第index个元素
  2. // assert isElementIndex(index);
  3. if (index < (size >> 1)) {
  4. Node<E> x = first;
  5. for (int i = 0; i < index; i++) 从链表第一个元素开始往后查找
  6. x = x.next;
  7. return x;
  8. } else {
  9. Node<E> x = last;
  10. for (int i = size - 1; i > index; i--) 从链表最后一个往前查找
  11. x = x.prev;
  12. return x;
  13. }
  14. }
  1. 每一个链表节点的数据结构:
  2. private static class Node<E> {
  3. E item; 当前元素
  4. Node<E> next; 下一个元素
  5. Node<E> prev; 上一个元素
  6.  
  7. Node(Node<E> prev, E element, Node<E> next) {
  8. this.item = element;
  9. this.next = next;
  10. this.prev = prev;
  11. }
  12. }
  1. private E unlinkFirst(Node<E> f) { 将第一个元素从链表的关联中去除
  2. // assert f == first && f != null;
  3. final E element = f.item;
  4. final Node<E> next = f.next;
  5. f.item = null;
  6. f.next = null; // help GC
  7. first = next;
  8. if (next == null)
  9. last = null;
  10. else
  11. next.prev = null;
  12. size--;
  13. modCount++;
  14. return element;
  15. }
  16. /**
  17. * Unlinks non-null last node l.
  18. */
  19. private E unlinkLast(Node<E> l) { 将最后一个元素从链表的关联中去除
  20. // assert l == last && l != null;
  21. final E element = l.item;
  22. final Node<E> prev = l.prev;
  23. l.item = null;
  24. l.prev = null; // help GC
  25. last = prev;
  26. if (prev == null)
  27. first = null;
  28. else
  29. prev.next = null;
  30. size--;
  31. modCount++;
  32. return element;
  33. }
  1. private void linkFirst(E e) { 创建一个新的节点,与第一个节点关联起来
  2. final Node<E> f = first;
  3. final Node<E> newNode = new Node<>(null, e, f); 创建的新节点后向指针指向第一个节点
  4. first = newNode;
  5. if (f == null)
  6. last = newNode;
  7. else
  8. f.prev = newNode; 将第一个节点对的前向指针指向新创建的节点
  9. size++;
  10. modCount++;
  11. }
  12.  
  13. /**
  14. * Links e as last element.
  15. */
  16. void linkLast(E e) { 创建一个新的节点,与最后一个节点关联起来
  17. final Node<E> l = last;
  18. final Node<E> newNode = new Node<>(l, e, null); 将新创建的节点指向最后的节点
  19. last = newNode;
  20. if (l == null)
  21. first = newNode;
  22. else
  23. l.next = newNode; 将那个最后的节点指向新创建的节点
  24. size++;
  25. modCount++;
  26. }
  1. public int indexOf(Object o) { 返回元素o在链表中的索引 若是不存在则返回-1
  2. int index = 0;
  3. if (o == null) {
  4. for (Node<E> x = first; x != null; x = x.next) {
  5. if (x.item == null) 若是需要查找的元素为null,就遍历链表中的值是否有null
  6. return index;
  7. index++;
  8. }
  9. } else {
  10. for (Node<E> x = first; x != null; x = x.next) {
  11. if (o.equals(x.item)) 查找元素的索引
  12. return index;
  13. index++;
  14. }
  15. }
  16. return -1;
  17. }
  1. public void clear() { 对链表进行清除
  2. // Clearing all of the links between nodes is "unnecessary", but:
  3. // - helps a generational GC if the discarded nodes inhabit
  4. // more than one generation
  5. // - is sure to free memory even if there is a reachable Iterator
  6. for (Node<E> x = first; x != null; ) {
  7. Node<E> next = x.next;
  8. x.item = null; 置为null GC会对内存进行回收
  9. x.next = null;
  10. x.prev = null;
  11. x = next;
  12. }
  13. first = last = null;
  14. size = 0;
  15. modCount++;
  16. }

集合-LinkList的更多相关文章

  1. collection set

    http://blog.csdn.net/humingfiy/article/details/7946408 Collection:List.SetMap:HashMap.HashTable 如何在它 ...

  2. Java学习历程记录(一)

    一.类与对象 1.创建类 创建一个学生类.并且创造成员变量和方法 public class student{ String name: int age: public void study(参数列表) ...

  3. Java集合(2)一 ArrayList 与 LinkList

    目录 Java集合(1)一 集合框架 Java集合(2)一 ArrayList 与 LinkList Java集合(3)一 红黑树.TreeMap与TreeSet(上) Java集合(4)一 红黑树. ...

  4. Java集合之ArrayList与LinkList

    注:示例基于JDK1.8版本 参考资料:Java知音公众号 本文超长,也是搬运的干货,希望小伙伴耐心看完. Collection集合体系 List.Set.Map是集合体系的三个接口. 其中List和 ...

  5. java集合系列之LinkList

    概要  第1部分 LinkedList介绍第2部分 LinkedList数据结构第3部分 LinkedList源码解析(基于JDK1.6.0_45) 第5部分 LinkedList示例 转载请注明出处 ...

  6. Java集合篇二:LinkList

    package com.test.collection; /** * 自定义实现LinkList * * 1.双向链表 * 实现原理:底层封装Node节点对象(每个节点存放有3部分内容:1.上个节点的 ...

  7. Java集合之LinkedHashMap

    一.初识LinkedHashMap 上篇文章讲了HashMap.HashMap是一种非常常见.非常有用的集合,但在多线程情况下使用不当会有线程安全问题. 大多数情况下,只要不涉及线程安全问题,Map基 ...

  8. java从基础知识(七)java集合

    一.集合类介绍 1.List(元素有放入顺序,可重复) 1.1.List的实现 1.1.1.ArrayList ArrayList就是动态数组(需要连续的存储空间),用MSDN中的说法,就是Array ...

  9. 图解集合6:LinkedHashMap

    初识LinkedHashMap 上两篇文章讲了HashMap和HashMap在多线程下引发的问题,说明了,HashMap是一种非常常见.非常有用的集合,并且在多线程情况下使用不当会有线程安全问题. 大 ...

随机推荐

  1. OC 导入类 #import和@class 区别

    objective-c中#import和@class的区别 在Objective-C中,可以使用#import和@class来引用别的类型, 但是你知道两者有什么区别吗? @class叫做forwar ...

  2. 常用的 Excel 函数

    概述 Excel 学的好,函数不可少.接下来就了解常用的函数. 首先作下简要说明: 本文的内容大多从网上搜集并加以个人理解整理而来,由于初学,可能会出现错误,如有欢迎指出: 所用演示软件为免费丑陋的 ...

  3. PAT (Basic Level) Practise (中文)-1032. 挖掘机技术哪家强(20)

    PAT (Basic Level) Practise (中文)-1032. 挖掘机技术哪家强(20) http://www.patest.cn/contests/pat-b-practise/1032 ...

  4. javase(5)_面向对象

    一.概述 1.面向对象是一种思想,让我们由执行者变成指挥者,执行者是面向过程,指挥者是面向对象.例如人开冰箱门,开冰箱门这个动作应该属于门而不是人,冰箱自己最清楚门应该怎么开,人只是调用了冰箱的这个动 ...

  5. 计算机应用 office系列 常用术语英文

    首先,Excel 办公室系列软件——Office series Software 微软——Microsoftware 电子表格 Excel 第一行称为标题栏——title bar 第二行称为菜单栏—— ...

  6. iptables(1)工具详解

    一. iptables 查看链表,创建链表,类命令 1. iptables [-t table] -N chain : 创建一条自定义规则的链 1 2     # iptables -t filter ...

  7. Python学习记录1

    交互式解释器 模块 python序列 索引提取 序列运算 空列表 成员资格 长度最大值最小值函数 列表 list和join函数 交互式解释器 ' >>> '为提示符. 语句是用来告诉 ...

  8. UVa-156-反片语

    这题比较精妙的是,我们对于单词重排,实际上是进行了标准化的处理,即按照字典序排序. 这样的话,就很方便地处理了单词的重排问题,我们不需要使用全排列函数进行排列尝试,我们直接化简为一,然后进行比较就可以 ...

  9. python面向对象(C3算法)(六)

    1. 了解python2和python3类的区别 python2在2.3之前使用的是经典类, 2.3之后, 使用的是新式类 2. 经典类的MRO 树形结构的深度优先遍历 -> 树形结构遍历 cl ...

  10. loj2002 「SDOI2017」序列计数

    水题 #include <iostream> #include <cstring> #include <cstdio> using namespace std; t ...