集合-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是一种非常常见.非常有用的集合,并且在多线程情况下使用不当会有线程安全问题. 大 ...
随机推荐
- 模拟水题之unique两行AC
https://icpc.njust.edu.cn/Contest/749/A/ Description 小鱼喜欢吃糖果.他有两盒糖果,两盒糖果分别仅由小写字母组成的字符串s和字符串t构成.其中'a' ...
- iOS开发资源:推送通知相关开源项目--PushSharp、APNS-PHP以及Pyapns等
PushSharp (github) PushSharp是一个实现了由服务器端向移动客户端推送消息的开源C#库,支持 iOS (iPhone/iPad APNS). Android (C2DM/GC ...
- eltwise层
http://blog.csdn.net/u013989576/article/details/73294131 layer { name: "fuse" type: " ...
- pb2.text_format.Merge(f.read(), self.solver_param) AttributeError: 'module' object has no attribute 'text_format'
http://blog.csdn.net/qq_33202928/article/details/72526710
- CPP-基础:运算符重载详解
1.运算符重载定义: C++中预定义的运算符的操作对象只能是基本数据类型.但实际上,对于许多用户自定义类型(例如类),也需要类似的运算操作.这时就必须在C++中重新定义这些运算符,赋予已有运算符新的功 ...
- Feign-请求不同注册中心的服务
场景 需要通过Feign Client请求,其他注册中心或者其他Restful服务. 临时方案 Feign 请求转为RestTemplate http请求. 优点:能适应,feign环境和非feign ...
- Bootstrap 网格系统(Grid System)实例4
Bootstrap 网格系统(Grid System)实例4:中型和大型设备 <!DOCTYPE html><html><head><meta http-eq ...
- 用函数式编程思维解析anagrams函数
//函数式编程思维分析 这个排列函数 const anagrams = str => { if (str.length <= 2) return str.length === 2 ? [s ...
- hihoCode-1043-完全背包
我们定义:best(i,x)代表i件以前的物品已经决定好选择多少件,并且在剩余奖券x的情况下的最优解. 我们可以考虑最后一步,是否再次选择i物品,在不超过持有奖券总额的情况下.上面的第二个式子的k是大 ...
- VS第一天(一堆错误的错误示范)
自学VS第一天 (目标用vs做个不low的简历) 学习视频 https://www.bilibili.com/video/av48489320/?p=1 代码 写了一天的代码,自己理解的内容在注释里 ...