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表达式使用的更多相关文章

  1. Lambda(一)lambda表达式初体验

    Lambda(一)lambda表达式初体验 Lambda引入 : 随着需求的不断改变,代码也需要随之变化 需求一:有一个农场主要从一堆苹果中挑选出绿色的苹果 解决方案:常规做法,source code ...

  2. 利用lambda和条件表达式构造匿名递归函数

    from operator import sub, mul def make_anonymous_factorial(): """Return the value of ...

  3. Util应用程序框架公共操作类(十二):Lambda表达式公共操作类(三)

    今天在开发一个简单查询时,发现我的Lambda操作类的GetValue方法无法正确获取枚举类型值,以至查询结果错误. 我增加了几个单元测试来捕获错误,代码如下. /// <summary> ...

  4. java8实战二------lambda表达式和函数式接口,简单就好

    一.Lambda 可以把Lambda表达式理解为简洁地i表示可传递的匿名函数的一种方式:它没有名称,但它有参数列表.函数主体.返回类型,可能还是一个可以抛出的异常列表. 听上去,跟我们用的匿名类,匿名 ...

  5. 二 lambda表达式

    1:lambda写的好可以极大的减少代码冗余,同时可读性也好过冗长的内部类,匿名类. 2: lambda表达式配合Java8新特性Stream API可以将业务功能通过函数式编程简洁的实现. 3: l ...

  6. [2014-12-30]如何动态构造Lambda表达式(动态构造Lambda查询条件表达式)

    声明 本文对Lambda表达式的扩展,示例代码来源于网络. 场景描述 web开发查询功能的时候,如果查询条件比较多,就会遇到动态组合查询条件的情况.在手写sql的情况下,我们一般会根据传入的参数,针对 ...

  7. lambda高级进阶--表达式参数

    1,现在我们封装一个方法,来提供一个比较器,显然比较器是拥有两个参数的--用来比较的两个值. public class Linkin { public static String[] sort(Str ...

  8. Day14--Python--函数二,lambda,sorted,filter,map,递归,二分法

    今日主要内容:1. lambda 匿名函数 lambda 参数: 返回值-------------------------------------def square(x): return x**2 ...

  9. C# 表达式树 创建、生成、使用、lambda转成表达式树~表达式树的知识详解

    笔者最近学了表达式树这一部分内容,为了加深理解,写文章巩固知识,如有错误,请评论指出~ 表达式树的概念 表达式树的创建有 Lambda法 和 组装法. 学习表达式树需要 委托.Lambda.Func& ...

随机推荐

  1. FCC---Create Movement Using CSS Animation---设计一个盒子上下左右移动,结合animation, @keyframe, position (上下左右的offset)

    When elements have a specified position, such as fixed or relative, the CSS offset properties right, ...

  2. js验证手机号、身份证等

    //验证手机号function check_lxdh(lxdh){ var mobile = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0 ...

  3. QT5.5+VS2013编译安装QtCharts (ZZ)

    环境 1.Windows 10 -x64: 2.MSVC 2013 -x64: 3.Qt5.5.1 -x86 and -x64. 编译过程 准备工作 1.安装ActivePerl 安装过程同一般软件安 ...

  4. Android App自动更新解决方案(DownloadManager)

    一开始,我们先向服务器请求数据获取版本 public ObservableField<VersionBean> appVersion = new ObservableField<&g ...

  5. SQL学习_SQL函数

    常用的 SQL 函数 1. 算术函数 SELECT ABS(-2),运行结果为 2 SELECT MOD(101,3),运行结果 2 SELECT ROUND(37.25,1),运行结果 37.3 2 ...

  6. [b0007] windows 下 eclipse 开发 hdfs程序样例

    目的: 学习使用hdfs 的java命令操作 相关: 进化: [b0010] windows 下 eclipse 开发 hdfs程序样例 (二) [b0011] windows 下 eclipse 开 ...

  7. vue+hammer.js完美实现长按、左滑,右滑等触控事件

    移动端使用手指直接操作的方式大受用户欢迎,这其中就包括了单点.多点.长按.双击等方式. 这么多触控事件,如果开发者自己实现,会浪费大量的时间和精力,快来看看 hammer.js 让我们轻松了多少吧. ...

  8. gdb x命令使用方法

    x命令是直接查看指定地址为开头的内存里的内容 既然是要看,就分你想怎么看,和看多少 怎么看: d 按十进制格式显示 x 按十六进制格式显示 a 按十六进制格式显示 u 按十六进制格式显示无符号整型 o ...

  9. Linux的启动过程的分析

    Linux的启动过程 Linux系统从启动大哦提供服务的基本过程为:首先机器家电,然后通过MBR或者UEFI装载GRUB,再启动内核,再由内核启动服务,最后开始对外服务 CentOS7要经历四个主要阶 ...

  10. 提升ML.NET模型的准确性

    ML.NET是一个面向.NET开发人员的开源.跨平台的机器学习框架. 使用ML.NET,您可以轻松地为诸如情绪分析.价格预测.销售分析.推荐.图像分类等场景构建自定义机器学习模型. ML.NET从0. ...