Lambda(二)lambda表达式使用
Lambda(二)lambda表达式使用
Lambda 表达式组成:
/*
param list arrow lambda body
(o1,o2) -> o1.getColor().CompareTo(o2.getColor());
*/
Lambda表达式需要有与之相匹配的预定义函数式接口:
/*
FunctionalInterface接口:Predicate<T>
方法:Boolean test(T t); FunctionalInterface接口:Consume<T>
方法:void accept(T t); FunctionalInterface接口:Function<T,R>
方法:R apply(T t); FunctionalInterface接口:Suppiler<T>
方法:T get();
*/
简单使用案例,source code 如下
Consumer<String> c = s -> System.out.println(s); Function<String,Integer> lambda = s->s.length(); Predicate<Apple> predicate = a->a.getColor().equals("green"); Function<Apple,Boolean> function = a->a.getColor().equals("red"); Supplier<Apple> instance = Apple::new;
假如,现在要对Apple的list进行排序(常规vsLambda):
//implement item1
list.sort(new Comparator<Apple>() {
@Override
public int compare(Apple o1, Apple o2) {
return o1.getColor().compareTo(o2.getColor());
}
}); //implement item2
list.sort((o1, o2) -> o1.getColor().compareTo(o2.getColor())); //implement item2
list.sort(comparing(Apple::getColor));
自定义使用,source code如下
public static List<Apple> filter(List<Apple> apples, Predicate<Apple> predicate){
List<Apple> result = new ArrayList<>();
for(Apple a:apples){
if(predicate.test(a)){
result.add(a);
}
}
return result;
} public static void main(String[] args) {
List<Apple> apples = Arrays.asList(new Apple("green", 120), new Apple("red", 150));
List<Apple> green = filter(apples, a -> a.getColor().equals("green"));
System.out.println(green);
}
java 8 API中最多只有两个入参,假如有多个参数,可以自己定义,source code如下
//自定义FunctionalInterface接口
@FunctionalInterface
public interface ThreeFunction<T,U,K,R> {
R apply(T t,U u,K k);
}
//具体使用
ThreeFunction<String,Long,String,ComplexApple> tf = ComplexApple::new;
ComplexApple cp = tf.apply("yellow", 111L, "fushi");
System.out.println(cp);
方法推导/方法引用(MethodReference):
/**
* 使用方法推导的条件:
* 1、类的实例方法
* 2、对象的实例方法
* 3、构造函数
*/
public static <T> void useConsume(Consumer<T> consumer,T t){
consumer.accept(t);
}
useConsume(System.out::println,"chuangg"); List<Apple> apples = Arrays.asList(new Apple("red", 100),
new Apple("green", 150),
new Apple("abc", 110));
apples.stream().forEach(System.out::println);
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
int i = Integer.parseInt("123"); Function<String,Integer> f = Integer::parseInt;
Integer apply = f.apply("123");
System.out.println(apply); BiFunction<String, Integer, Character> f1 = String::charAt;
Character r1 = f1.apply("hello", 2);
System.out.println(r1); String s = new String("hello");
Function<Integer, Character> f2 = s::charAt;
Character r2 = f2.apply(1);
System.out.println(r2); Supplier<Apple> appleSupplier = Apple::new;
Apple apple = appleSupplier.get(); BiFunction<String,Long,Apple> appleBiFunction = Apple::new;
Apple blue = appleBiFunction.apply("blue", 123L);
System.out.println(blue);
Lambda(二)lambda表达式使用的更多相关文章
- Lambda(一)lambda表达式初体验
Lambda(一)lambda表达式初体验 Lambda引入 : 随着需求的不断改变,代码也需要随之变化 需求一:有一个农场主要从一堆苹果中挑选出绿色的苹果 解决方案:常规做法,source code ...
- 利用lambda和条件表达式构造匿名递归函数
from operator import sub, mul def make_anonymous_factorial(): """Return the value of ...
- Util应用程序框架公共操作类(十二):Lambda表达式公共操作类(三)
今天在开发一个简单查询时,发现我的Lambda操作类的GetValue方法无法正确获取枚举类型值,以至查询结果错误. 我增加了几个单元测试来捕获错误,代码如下. /// <summary> ...
- java8实战二------lambda表达式和函数式接口,简单就好
一.Lambda 可以把Lambda表达式理解为简洁地i表示可传递的匿名函数的一种方式:它没有名称,但它有参数列表.函数主体.返回类型,可能还是一个可以抛出的异常列表. 听上去,跟我们用的匿名类,匿名 ...
- 二 lambda表达式
1:lambda写的好可以极大的减少代码冗余,同时可读性也好过冗长的内部类,匿名类. 2: lambda表达式配合Java8新特性Stream API可以将业务功能通过函数式编程简洁的实现. 3: l ...
- [2014-12-30]如何动态构造Lambda表达式(动态构造Lambda查询条件表达式)
声明 本文对Lambda表达式的扩展,示例代码来源于网络. 场景描述 web开发查询功能的时候,如果查询条件比较多,就会遇到动态组合查询条件的情况.在手写sql的情况下,我们一般会根据传入的参数,针对 ...
- lambda高级进阶--表达式参数
1,现在我们封装一个方法,来提供一个比较器,显然比较器是拥有两个参数的--用来比较的两个值. public class Linkin { public static String[] sort(Str ...
- Day14--Python--函数二,lambda,sorted,filter,map,递归,二分法
今日主要内容:1. lambda 匿名函数 lambda 参数: 返回值-------------------------------------def square(x): return x**2 ...
- C# 表达式树 创建、生成、使用、lambda转成表达式树~表达式树的知识详解
笔者最近学了表达式树这一部分内容,为了加深理解,写文章巩固知识,如有错误,请评论指出~ 表达式树的概念 表达式树的创建有 Lambda法 和 组装法. 学习表达式树需要 委托.Lambda.Func& ...
随机推荐
- Jquery选择器个人总结
1.选择第一级子节点 通过> 或者children方法实现 $('#XtraTabPage8>.datagrid-ftable') $('#XtraTabPage8').children( ...
- mysql-操作篇
# ### mysqlctrl + l 清屏ctrl + c 终止[linux]service mysql start 启动mysqlservice mysql stop 停止mysqlservice ...
- PyCharm颜色设置
选择主题和背景图片 选择字体.修改字体大小 新建颜色主题 修改背景颜色 修改注释颜色 File --> Setting (Ctrl + Shift + S) 1.选择不同的主题.选择背景图片 A ...
- CodeForces - 1257E (思维)
题意 https://vjudge.net/problem/CodeForces-1257E 三个人,每个人有一些数字,组合起来是1~n,每个人可以给另一个人一个拥有的数字,问最小操作数,使得第一个人 ...
- JAVA反射(资源版)
关于JAVA反射的作用可以看这篇博客(说的很详尽): https://www.cnblogs.com/jqyp/archive/2012/03/29/2423112.html 下面是关于JAVA反射的 ...
- 6.1 Spark SQL
一.从shark到Spark SQL Hive能够把SQL程序转换成map-reduce程序 可以把Hadoop中的Hive看作是一个接口,主要起到了转换的功能,并没有实际存储数据. Shark即 ...
- 2. java 运算符
运算符 一.算术运算符 1. 四则与取模 + - * / % ++ -- (1) 单独使用++/--,前++和后++没有任何区别. (2) 混合使用,有区别 ①如果是前++,那么变量立刻马上 +1,然 ...
- StringBuild
* StringBuild 初始容量是 32 * 1.属性: * Length:现有内容长度: * Capacity:StringBuild当前最大容量 * * 2. * 通过 设置Length=10 ...
- mysql 高级查询二
各种showshow columns from my_student;show grants for root;show aviables;show processlist;show table st ...
- node.js是用来做什么的?这是我看到最好的解释了
一种JavaScript的运行环境,能够使得JavaScript脱离浏览器运行. 参考链接:https://www.cnblogs.com/suhaihong/p/6598308.html https ...