J2SE 8的Lambda --- functions
functions
//1. Runnable 输入参数:无 返回类型void
new Thread(() -> System.out.println("In Java8!") ).start();
System.out.println();
//2.Predicate<T> 输入参数:t 返回类型boolean
List<String> languages = Arrays.asList("Java", "Scala", "C++", "Haskell", "Lisp");
// case1, 用流fliter,再 foreach遍历,传入Predicate 判断boolean
System.out.println("Languages which starts with J :");
filter(languages, (str)->str.startsWith("J"));
System.out.println();
Predicate<String> jSatrtConditions =(str)->str.startsWith("J");
Predicate<String> aEndsConditions =(str)->str.endsWith("a");
//case2, 两个&&关系
filter(languages, jSatrtConditions.and(aEndsConditions));
System.out.println();
//case3, 两个||关系
filter(languages, jSatrtConditions.or(aEndsConditions));
System.out.println();
//case4, 调用Predicate的静态isEqual方法
Predicate<String> predicateEqual = Predicate.isEqual("Java");
System.out.println(predicateEqual.test("JAVA"));
Predicate<String> and = predicateEqual.and(jSatrtConditions);
filter(languages, and);
System.out.println();
//3.Function<T,R> 输入参数:T 返回类型R
List<Integer> numList = Arrays.asList(100, 200, 300, 400, 500);
filter2(numList, n->n+1);
System.out.println();
//4.BiFunction<T, U, R> 输入参数:T,U 返回类型R
BiFunction<String, String,String> biFunction = (x,y)->x+y;
System.out.println(biFunction.apply("111", "222"));
System.out.println(biFunction.andThen(n->n+"*").apply("111", "222"));
System.out.println();
//5.BinaryOperator<T,T,T> 输入参数:T,T 返回类型T
//Represents an operation upon two operands of the same type, producing a result of the same type as the operands
Integer filter3Result = filter3(numList, n->n+1, (x,y)->x+y);
System.out.println(filter3Result);
System.out.println();
//第一步,和identity做相加生产新的数组
//第二步,新的数组每两个相乘
List<Integer> numList2 = Arrays.asList(1, 2, 3, 4, 5);
Integer filter4Result = filter4(numList2, 1, (x,y)->{
return x+y;
}, (x,y)->{
return x*y;
});
System.out.println(filter4Result);
System.out.println();
//6.Optional
//值不存在的情况下产生可替代物
ArrayList<String> arrayList = new ArrayList<>();
//(1) 创建Optional
//1) Optional.of() 它要求传入的 obj 不能是 null 值的, 否则还没开始进入角色就倒在了 NullPointerException 异常上了
Optional<String> stringOptional = Optional.of("stringOptional");
System.out.println(stringOptional.orElse(null));
System.out.println();
//2) Optional.ofNullable(obj): 它以一种智能的, 宽容的方式来构造一个 Optional 实例. 来者不拒, 传 null 进到就得到 Optional.empty(), 非 null 就调用 Optional.of(obj)
stringOptional = Optional.ofNullable(null);
System.out.println(stringOptional.orElse(null));
System.out.println();
//3) Optional.empty() 空的Optional
Optional<Object> emptyOptional = Optional.empty();
System.out.println(emptyOptional.orElse(null));
System.out.println();
//4) 自己创建
Optional<Double> inverseOptional = inverse(1.2);
System.out.println(inverseOptional.orElse(null));
System.out.println();
//(2) orElse() 值不存在的情况下的默认物
Optional<String> ofNullableOptional = Optional.ofNullable(null);
System.out.println(ofNullableOptional.orElse("default value"));
ofNullableOptional = Optional.empty();
System.out.println(ofNullableOptional.orElse("default value"));
System.out.println();
//(3) orElseGet() 值不存在的情况下, 提供Supplier
ofNullableOptional = Optional.ofNullable(null);
System.out.println(ofNullableOptional.orElseGet(()->new Date().toString()));
ofNullableOptional = Optional.empty();
System.out.println(ofNullableOptional.orElseGet(()->new Date().toString()));
System.out.println();
//(4) orElseThrow() 值不存在的情况下, 提供Supplier异常
try {
ofNullableOptional = Optional.ofNullable(null);
System.out.println(ofNullableOptional.orElseThrow(Exception::new));
} catch (Exception e) {
System.out.println("orElseThrow");
e.printStackTrace();
}
System.out.println();
try {
ofNullableOptional = Optional.empty();
System.out.println(ofNullableOptional.orElseThrow(Exception::new));
} catch (Exception e) {
System.out.println("orElseThrow");
e.printStackTrace();
}
System.out.println();
//(5) isPresent() 是否存在
boolean present = Optional.of("stringOptional").isPresent();
System.out.println(present);
present = Optional.empty().isPresent();
System.out.println(present);
System.out.println();
//(6) ifPresent() 如果存在,将元素传递给对应函数(比如将其加入集合或者做计算)
Optional.of("stringOptional").ifPresent(e->System.out.println(e));
Optional.of("stringOptional").ifPresent(System.out::println);
Optional.of("stringOptional").ifPresent(e->arrayList.add(e));
Optional.empty().ifPresent(e->System.out.println("xxx"));
System.out.println();
//(7) map() 将对应的值传递给mapper
Optional.of("map").map(e->arrayList.add(e));
System.out.println(arrayList);
Optional<Boolean> mapOptional = Optional.ofNullable("mapOptional").map(arrayList::add);
System.out.println(arrayList);
mapOptional = Optional.of("map").map(e->arrayList.add(e));
System.out.println(mapOptional.orElse(null));
//(8) get() 得到对应的值, 如果值不存在, 抛出exception
System.out.println(Optional.of("map").get());
//java.util.NoSuchElementException: No value present
// System.out.println(Optional.ofNullable(null).get());
Optional<Object> ofNullable = Optional.ofNullable(null);
if(ofNullable.isPresent()){
System.out.println(ofNullable.get());
}
//(9) flatMap() 组合两个返回Optional的方法 f(), g()
//方式一: f().float(T::g)
Optional<Double> flatMapOptional = inverse(1.2).flatMap(LambdaFunctionsTest::square);
System.out.println(flatMapOptional.orElse(null));
System.out.println();
//方式二: .float(T::f).float(T::g) 还可以在后面再.float(T::g)
flatMapOptional = Optional.of(1.2).flatMap(LambdaFunctionsTest::inverse).flatMap(LambdaFunctionsTest::square);
System.out.println(flatMapOptional.orElse(null));
System.out.println();
辅助方法
public static <T> void filter(List<T> names, Predicate<T> conditions){
names.stream().filter(conditions).forEach(name->System.out.println(name));
}
public static <T,R> void filter2(List<T> names, Function<T,R> function){
names.stream().map(function).forEach(name->System.out.println(name));
}
public static <T,R> R filter3(List<T> names, Function<T,R> mapFunction, BinaryOperator<R> binaryOperatorFunction){
return names.stream().map(mapFunction).reduce(binaryOperatorFunction).get();
}
public static <T,U,R> U filter4(List<T> names, U identity, BiFunction<U, ? super T, U> accumulator, BinaryOperator<U> binaryOperatorFunction){
return names.stream().parallel().reduce(identity, accumulator, binaryOperatorFunction);
}
public static Optional<Double> inverse(Double t){
return null==t||t.equals(0) ? Optional.empty():Optional.of(1/t);
}
public static Optional<Double> square(Double t){
return null==t||t.equals(0) ? Optional.empty():Optional.of(Math.sqrt(t));
}
J2SE 8的Lambda --- functions的更多相关文章
- C++闭包: Lambda Functions in C++11
表达式无疑是C++11最激动人心的特性之一!它会使你编写的代码变得更优雅.更快速! 它实现了C++11对于支持闭包的支持.首先我们先看一下什么叫做闭包 维基百科上,对于闭包的解释是: In progr ...
- Python: Lambda Functions
1. 作用 快速创建匿名单行最小函数,对数据进行简单处理, 常常与 map(), reduce(), filter() 联合使用. 2. 与一般函数的不同 >>> def f (x) ...
- J2SE 8的Lambda --- 语法
语法例子 LambdaGrammarTest lambdaTest = new LambdaGrammarTest(); // 1. 能够推导出类型的,可以不写类型 String[] planets ...
- J2SE 8的Lambda --- Comparator
Person[] personArray = new Person[]{new Person("Tom"),new Person("Jack"),new Per ...
- fn project AWS Lambda 格式 functions
Creating Lambda Functions Creating Lambda functions is not much different than using regular funct ...
- 在Android中使用Java 8的lambda表达式
作为一名Java开发者,或许你时常因为缺乏闭包而产生许多的困扰.幸运的是:Java's 8th version introduced lambda functions给我们带来了好消息;然而,这咩有什 ...
- Python中Lambda, filter, reduce and map 的区别
Lambda, filter, reduce and map Lambda Operator Some like it, others hate it and many are afraid of t ...
- Php5.3的lambda函数以及closure(闭包)
从php5.3以后,php也可以使用lambda function(可能你会觉得是匿名函数,的确是但不仅仅是)来写类似javascript风格的代码: $myFunc = function() { e ...
- Automated EBS Snapshots using AWS Lambda & CloudWatch
Overview In this post, we'll cover how to automate EBS snapshots for your AWS infrastructure using L ...
随机推荐
- 多线程安全问题之Lock显示锁
package com.hls.juc; import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.Reentr ...
- BitKeeper 和 Git
在 2002 年到 2005年, Linux 内核使用 BitKeeper 管理代码. BitKeeper 的 CEO 和 Linus 曾经是好友. 在 Git 诞生 11年后 BitKeeper 宣 ...
- mac下hbase安装
出处:https://www.jianshu.com/p/510e1d599123 安装到的路径:/usr/local/Cellar/hbase/1.2.6 linux操作: linux命令 作用 . ...
- gitlab-ce-omnibus社区版的备份、还原及升级
gitlab-ce-omnibus社区版的备份和还原,可以使用gitlab自带工具,gitlab-rake来完成,详见下面例子 将旧gitlab服务器备份,并还原至新gitlab服务器 ,这两台git ...
- Debug---Eclipse断点调试基础
1.进入debug模式(基础知识列表)1.设置断点 2.启动servers端的debug模式 3.运行程序,在后台遇到断点时,进入debug调试状态 ========================= ...
- JZ2440 裸机驱动 第8章 NAND Flash控制器
本章目标 了解NAND Flash 芯片的接口 掌握通过NAND Flash控制器访问NAND Flash的方法 8.1 NAND Flash介绍和NAND Flash控制器使用 NAND ...
- asp.net Repeater使用例子,包括分页
<style type="text/css"> .tab{border-collapse:collapse; margin:0 auto;} .tab th ...
- PHP-redis api 中文说明(转)
来源 : http://hi.baidu.com/gaolamp/item/1686aac07334bd0f0ad93a9f PHP-redis api 中文说明 phpredis 是 php 的一个 ...
- gc之六--Minor GC、Major GC、Full GC以及Mixed GC之间的区别
目录: GC之一--GC 的算法分析.垃圾收集器.内存分配策略介绍 GC之二--GC日志分析(jdk1.8)整理中 GC之三--GC 触发Full GC执行的情况及应对策略 gc之四--Minor G ...
- poj 2229 Sumsets(dp)
Sumsets Time Limit : 4000/2000ms (Java/Other) Memory Limit : 400000/200000K (Java/Other) Total Sub ...