Lambda表达式类似于函数引用,也是匿名内部类,主要有三种形式
现有对象的实例方法引用
String aString="123";
Supplier<Integer> supplier=aString::length;
任意方法引用
Function<String, Integer>stringIntegerFunction = String::length;
静态方法引用
Function<String, Integer> function =Integer::parseInt;
中间操作只是对操作进行了记录,只有结束操作才会触发实际的计算(即惰性求值),这也是Stream在迭代大集合时高效的原因之一。
中间操作又可以分为无状态(Stateless)操作与有状态(Stateful)操作,
前者是指元素的处理不受之前元素的影响;
后者是指该操作只有拿到所有元素之后才能继续下去。
结束操作又可以分为短路与非短路操作,
前者是指遇到某些符合条件的元素就可以得到最终结果;
而后者是指必须处理所有元素才能得到最终结果。
自定义类似Consumer函数式接口
Actionaction = System.out ::println;
action.execute("HelloWorld!");
test(System.out ::println, "Hello World!");
}
staticvoid test(Actionaction, String str) {
action.execute(str);
}
@FunctionalInterface
interfaceAction<T> {
publicvoid execute(Tt);
}
CompletableFuture如果两个future是依赖关系,第二个依赖于第一个,则使用thenCompose