集合-LinkList
参考: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
- public class ConsumerTest {
- public static void main(String[] args) {
- //testConsumer();
- testAndThen();
- }
- public static void testConsumer(){
- Consumer<Integer> square=x->System.out.println("print square: "+x*x);
- square.accept(2);
- }
- public static void testAndThen() {
- Consumer<Integer> consumer1=x->System.out.println("first x :"+x);
- Consumer<Integer> consumer2=x->{
- System.out.println("second x: "+x);
- throw new NullPointerException("throw exception test");
- };
- Consumer<Integer> consumer3=x->System.out.println("third x: "+x);
- consumer1.andThen(consumer2).andThen(consumer3).accept(1);
- }
- }
函数式编程与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)
- public boolean addAll(int index, Collection<? extends E> c) { 将一个集合c插入到链表中,index的位置
- checkPositionIndex(index);
- Object[] a = c.toArray();
- int numNew = a.length;
- if (numNew == 0)
- return false;
- Node<E> pred, succ;
- if (index == size) { 若是插入到最后一个元素的后面
- succ = null; 这个元素指向null
- pred = last; 这个元素的前向指针指向之前的最后一个元素
- } else {
- succ = node(index); 这个元素后向指针指向原来index处的元素
- pred = succ.prev; 这个元素前向指针指向原来index处元素的前一个元素
- }
- 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;
- }
- Node<E> node(int index) { 这里采用二分法去查找 链表上第index个元素
- // assert isElementIndex(index);
- if (index < (size >> 1)) {
- Node<E> x = first;
- for (int i = 0; i < index; i++) 从链表第一个元素开始往后查找
- x = x.next;
- return x;
- } else {
- Node<E> x = last;
- for (int i = size - 1; i > index; i--) 从链表最后一个往前查找
- x = x.prev;
- return x;
- }
- }
- 每一个链表节点的数据结构:
- 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;
- }
- }
- private E unlinkFirst(Node<E> f) { 将第一个元素从链表的关联中去除
- // assert f == first && f != null;
- final E element = f.item;
- final Node<E> next = f.next;
- f.item = null;
- f.next = null; // help GC
- first = next;
- if (next == null)
- last = null;
- else
- next.prev = null;
- size--;
- modCount++;
- return element;
- }
- /**
- * Unlinks non-null last node l.
- */
- private E unlinkLast(Node<E> l) { 将最后一个元素从链表的关联中去除
- // assert l == last && l != null;
- final E element = l.item;
- final Node<E> prev = l.prev;
- l.item = null;
- l.prev = null; // help GC
- last = prev;
- if (prev == null)
- first = null;
- else
- prev.next = null;
- size--;
- modCount++;
- return element;
- }
- private void linkFirst(E e) { 创建一个新的节点,与第一个节点关联起来
- final Node<E> f = first;
- final Node<E> newNode = new Node<>(null, e, f); 创建的新节点后向指针指向第一个节点
- first = newNode;
- if (f == null)
- last = newNode;
- else
- f.prev = newNode; 将第一个节点对的前向指针指向新创建的节点
- size++;
- modCount++;
- }
- /**
- * Links e as last element.
- */
- void linkLast(E e) { 创建一个新的节点,与最后一个节点关联起来
- final Node<E> l = last;
- final Node<E> newNode = new Node<>(l, e, null); 将新创建的节点指向最后的节点
- last = newNode;
- if (l == null)
- first = newNode;
- else
- l.next = newNode; 将那个最后的节点指向新创建的节点
- size++;
- modCount++;
- }
- public int indexOf(Object o) { 返回元素o在链表中的索引 若是不存在则返回-1
- int index = 0;
- if (o == null) {
- for (Node<E> x = first; x != null; x = x.next) {
- if (x.item == null) 若是需要查找的元素为null,就遍历链表中的值是否有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;
- }
- 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; 置为null GC会对内存进行回收
- x.next = null;
- x.prev = null;
- x = next;
- }
- first = last = null;
- size = 0;
- modCount++;
- }
集合-LinkList的更多相关文章
- collection set
http://blog.csdn.net/humingfiy/article/details/7946408 Collection:List.SetMap:HashMap.HashTable 如何在它 ...
- Java学习历程记录(一)
一.类与对象 1.创建类 创建一个学生类.并且创造成员变量和方法 public class student{ String name: int age: public void study(参数列表) ...
- Java集合(2)一 ArrayList 与 LinkList
目录 Java集合(1)一 集合框架 Java集合(2)一 ArrayList 与 LinkList Java集合(3)一 红黑树.TreeMap与TreeSet(上) Java集合(4)一 红黑树. ...
- Java集合之ArrayList与LinkList
注:示例基于JDK1.8版本 参考资料:Java知音公众号 本文超长,也是搬运的干货,希望小伙伴耐心看完. Collection集合体系 List.Set.Map是集合体系的三个接口. 其中List和 ...
- java集合系列之LinkList
概要 第1部分 LinkedList介绍第2部分 LinkedList数据结构第3部分 LinkedList源码解析(基于JDK1.6.0_45) 第5部分 LinkedList示例 转载请注明出处 ...
- Java集合篇二:LinkList
package com.test.collection; /** * 自定义实现LinkList * * 1.双向链表 * 实现原理:底层封装Node节点对象(每个节点存放有3部分内容:1.上个节点的 ...
- Java集合之LinkedHashMap
一.初识LinkedHashMap 上篇文章讲了HashMap.HashMap是一种非常常见.非常有用的集合,但在多线程情况下使用不当会有线程安全问题. 大多数情况下,只要不涉及线程安全问题,Map基 ...
- java从基础知识(七)java集合
一.集合类介绍 1.List(元素有放入顺序,可重复) 1.1.List的实现 1.1.1.ArrayList ArrayList就是动态数组(需要连续的存储空间),用MSDN中的说法,就是Array ...
- 图解集合6:LinkedHashMap
初识LinkedHashMap 上两篇文章讲了HashMap和HashMap在多线程下引发的问题,说明了,HashMap是一种非常常见.非常有用的集合,并且在多线程情况下使用不当会有线程安全问题. 大 ...
随机推荐
- OC 导入类 #import和@class 区别
objective-c中#import和@class的区别 在Objective-C中,可以使用#import和@class来引用别的类型, 但是你知道两者有什么区别吗? @class叫做forwar ...
- 常用的 Excel 函数
概述 Excel 学的好,函数不可少.接下来就了解常用的函数. 首先作下简要说明: 本文的内容大多从网上搜集并加以个人理解整理而来,由于初学,可能会出现错误,如有欢迎指出: 所用演示软件为免费丑陋的 ...
- PAT (Basic Level) Practise (中文)-1032. 挖掘机技术哪家强(20)
PAT (Basic Level) Practise (中文)-1032. 挖掘机技术哪家强(20) http://www.patest.cn/contests/pat-b-practise/1032 ...
- javase(5)_面向对象
一.概述 1.面向对象是一种思想,让我们由执行者变成指挥者,执行者是面向过程,指挥者是面向对象.例如人开冰箱门,开冰箱门这个动作应该属于门而不是人,冰箱自己最清楚门应该怎么开,人只是调用了冰箱的这个动 ...
- 计算机应用 office系列 常用术语英文
首先,Excel 办公室系列软件——Office series Software 微软——Microsoftware 电子表格 Excel 第一行称为标题栏——title bar 第二行称为菜单栏—— ...
- iptables(1)工具详解
一. iptables 查看链表,创建链表,类命令 1. iptables [-t table] -N chain : 创建一条自定义规则的链 1 2 # iptables -t filter ...
- Python学习记录1
交互式解释器 模块 python序列 索引提取 序列运算 空列表 成员资格 长度最大值最小值函数 列表 list和join函数 交互式解释器 ' >>> '为提示符. 语句是用来告诉 ...
- UVa-156-反片语
这题比较精妙的是,我们对于单词重排,实际上是进行了标准化的处理,即按照字典序排序. 这样的话,就很方便地处理了单词的重排问题,我们不需要使用全排列函数进行排列尝试,我们直接化简为一,然后进行比较就可以 ...
- python面向对象(C3算法)(六)
1. 了解python2和python3类的区别 python2在2.3之前使用的是经典类, 2.3之后, 使用的是新式类 2. 经典类的MRO 树形结构的深度优先遍历 -> 树形结构遍历 cl ...
- loj2002 「SDOI2017」序列计数
水题 #include <iostream> #include <cstring> #include <cstdio> using namespace std; t ...