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& ...
随机推荐
- sqlmap的浅研究
sqlmap注入工具: sqlmap 是一个开源的渗透测试工具,他可以自动的检测和利用SQL注入漏洞:sqlmap配置了一个强大功能的检测引擎,如果URL存在注入漏洞,它就可以从数据库中提取数据,完成 ...
- 记录C#泛型
常见的泛型类型 泛型类 class MyClass<T> { //...... } 泛型接口 interface GenericInterface<T> { void Gene ...
- 获取SpringCloud gateway响应的response的值,记录踩坑
最近在做网关改造,想要通过Gateway过滤器获取ResponseBody的值,查看了网上的帖子和官网内容: 帖子:https://cloud.tencent.com/developer/articl ...
- 自动化测试中执行JS脚本方法封装
执行JS脚本方法封装: class JavaScript(Base): def execute_javascript(self, js): """执行 JavaScrip ...
- ES3、ES5、ES6对象代理的写法差异
ES3的对象代理写法: console.log('定义私有变量ES3写法:') // ES3 var Person = function (){ var data = { name:'ES3', ag ...
- python session保持登录,新增地址,并删除,由观察可知,address_id决定删除的内容;
import requests,reheaders={"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) Ap ...
- jmeter录制移动端脚本
jmeter录制脚本有两种方式,一种借助外部工具badbody,一种是本身的功能,使用代理服务器,介绍下如何使用代理服务器录制脚本.我一般在测app或者移动端H5页面时才会录制,所以此文也针对移动端. ...
- CF731C Socks
CF731C Socks 洛谷评测传送门 题目描述 Arseniy is already grown-up and independent. His mother decided to leave h ...
- LG4111/LOJ2122 「HEOI2015」小Z的房间 矩阵树定理
问题描述 LG4111 题解 矩阵树定理板子题. \(\mathrm{Code}\) #include<bits/stdc++.h> using namespace std; #defin ...
- 7.Java内存模型详解
https://blog.csdn.net/qq_37141773/article/details/103138476 一.虚拟机 同样的java代码在不同平台生成的机器码肯定是不一样的,因为不同的操 ...