1、Predicate/Consumer/Function/Supplier介绍

Predicate boolean test(T t);
Consumer accpet(T t);
Function<T, R> R apply(T t);
Supplier<T> T get();

以Predicate为例,引申出很多类似的Predicate,如IntPredicate、DoublePredicate、BiPredicate、LongPredicate。但是他们的用法都是差不多的。比较类似。

2、举例子:

package com.cy.java8;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.*; public class LambdaUsage { private static List<Apple> filter(List<Apple> source, Predicate<Apple> predicate){
List<Apple> result = new ArrayList<>();
for(Apple a : source){
if(predicate.test(a)){
result.add(a);
}
}
return result;
} //根据一个long类型的参数过滤
private static List<Apple> filterByWeight(List<Apple> source, LongPredicate predicate){
List<Apple> result = new ArrayList<>();
for(Apple a : source){
if(predicate.test(a.getWeight())){
result.add(a);
}
}
return result;
} //根据两个参数过滤
private static List<Apple> filterByColorWeight(List<Apple> source, BiPredicate<String, Long> bipredicate){
List<Apple> result = new ArrayList<>();
for(Apple a : source){
if(bipredicate.test(a.getColor(), a.getWeight())){
result.add(a);
}
}
return result;
} private static void simpleTestConsumer(List<Apple> source, Consumer<Apple> consumer){
for(Apple a : source){
consumer.accept(a);
}
} private static String testFunction(Apple apple, Function<Apple, String> fun){
return fun.apply(apple);
} public static void main(String[] args) {
List<Apple> list = Arrays.asList(new Apple("green", 120), new Apple("red", 150));
List<Apple> greenList = filter(list, apple -> apple.getColor().equals("green"));
System.out.println(greenList); System.out.println("-----------------------------");
List<Apple> weightList = filterByWeight(list, weight -> weight>=150);
System.out.println(weightList); System.out.println("-----------------------------");
List<Apple> result = filterByColorWeight(list, (color, weight) -> color.equals("red") && weight > 100);
System.out.println(result); System.out.println("-----------------------------");
simpleTestConsumer(list, apple -> System.out.println("print apple's string method: " +apple)); System.out.println("-----------------------------");
String color = testFunction(new Apple("yellow", 10), apple -> apple.getColor());
System.out.println(color); System.out.println("-----------------------------");
Supplier<String> supplier = String::new;
System.out.println(supplier.get().getClass());
} }

打印结果:

[Apple(color=green, weight=120)]
-----------------------------
[Apple(color=red, weight=150)]
-----------------------------
[Apple(color=red, weight=150)]
-----------------------------
print apple's string method: Apple(color=green, weight=120)
print apple's string method: Apple(color=red, weight=150)
-----------------------------
yellow
-----------------------------
class java.lang.String

3、方法推导解析      

什么情况下允许方法推导的方式来写呢?
1.可以通过一个类的静态方法,比如Integer::parseInt
2.可以通过一个类的成员方法。
3.可以通过一个类的实例的方法。
4.可以通过构造函数的推导。

举例子:

package com.cy.java8;

import lombok.Data;

@Data
public class ComplexApple {
private String color;
private long weight;
private String name; public ComplexApple(String color, long weight, String name) {
this.color = color;
this.weight = weight;
this.name = name;
}
}
package com.cy.java8;

@FunctionalInterface
public interface ThreeParamFuntion<T, U, K, R> { R apply(T t, U u, K k);
}
package com.cy.java8;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Stream; public class MethodReference { public static void main(String[] args) {
/*Consumer<String> consumer = s -> System.out.println(s);
useConsumer(consumer,"hello alex");*/ useConsumer(s -> System.out.println(s),"hello alex");
useConsumer(System.out::println, "hello today"); List<Apple> list = Arrays.asList(new Apple("green", 120), new Apple("abc", 100), new Apple("red", 150));
System.out.println(list);
list.sort((a1, a2) -> a1.getColor().compareTo(a2.getColor()));
System.out.println(list); //比如循环输出apple信息
list.stream().forEach(apple -> System.out.println(apple)); //可以改成成如下
System.out.println("-----------------------------------------");
list.stream().forEach(System.out::println);
System.out.println("-----------------------------------------"); //方法推导,通过一个类的静态方法
Function<String, Integer> funtion = Integer::parseInt;
int result = funtion.apply("123");
System.out.println(result); //方法推导,通过一个类的成员方法
BiFunction<String, Integer, Character> f1 = String::charAt;
System.out.println(f1.apply("index", 0)); //方法推导,通过一个类的实例的方法
String s = new String("index");
Function<Integer, Character> f2 = s::charAt;
System.out.println(f2.apply(1)); //通过构造函数的推导
Supplier<String> s1 = String::new;
System.out.println(s1.get().getClass().getSimpleName()); BiFunction<String, Long, Apple> biFunction = Apple::new;
System.out.println(biFunction.apply("pink", 2L)); //三个参数的构造方法,ComplexApple::new,需要自己定义FunctionalInterface
ThreeParamFuntion<String, Long, String, ComplexApple> threeParamFuntion = ComplexApple::new;
System.out.println(threeParamFuntion.apply("black", 1L, "blackApple")); //再次看下上面list的排序的另一种方法
List<Apple> list2 = Arrays.asList(new Apple("green", 120), new Apple("abc", 100), new Apple("red", 150));
list2.sort(Comparator.comparing(Apple::getColor));
System.out.println(list2);
} private static <T> void useConsumer(Consumer<T> consumer, T t){
consumer.accept(t);
consumer.accept(t);
} }

打印如下:

hello alex
hello alex
hello today
hello today
[Apple(color=green, weight=120), Apple(color=abc, weight=100), Apple(color=red, weight=150)]
[Apple(color=abc, weight=100), Apple(color=green, weight=120), Apple(color=red, weight=150)]
Apple(color=abc, weight=100)
Apple(color=green, weight=120)
Apple(color=red, weight=150)
-----------------------------------------
Apple(color=abc, weight=100)
Apple(color=green, weight=120)
Apple(color=red, weight=150)
-----------------------------------------
123
i
n
String
Apple(color=pink, weight=2)
ComplexApple(color=black, weight=1, name=blackApple)
[Apple(color=abc, weight=100), Apple(color=green, weight=120), Apple(color=red, weight=150)]

lambda表达式使用解析的更多相关文章

  1. Lambda表达式树解析(下)

    概述 前面章节,总结了Lambda树的构建,那么怎么解析Lambda表达式树那?Lambda表达式是一种委托构造而成,如果能够清晰的解析Lambda表达式树,那么就能够理解Lambda表达式要传递的正 ...

  2. Lambda表达式树解析(下)包含自定义的provider和查询

    概述 前面章节,总结了Lambda树的构建,那么怎么解析Lambda表达式树那?Lambda表达式是一种委托构造而成,如果能够清晰的解析Lambda表达式树,那么就能够理解Lambda表达式要传递的正 ...

  3. 今夜我懂了Lambda表达式_解析

    现在时间午夜十一点~ 此刻的我血脉喷张,异常兴奋:因为专注得学习了一把java,在深入集合的过程中发现好多套路配合Lambda表达式真的是搜椅子,so开了个分支,决定从"只认得", ...

  4. Python 中Lambda 表达式 实例解析

    Lambda 表达式 lambda表达式是一种简洁格式的函数.该表达式不是正常的函数结构,而是属于表达式的类型.而且它可以调用其它函数. 1.基本格式: lambda 参数,参数...:函数功能代码 ...

  5. C++11 Lambda表达式简单解析

    C++11 新增了非常多特性,lambda 表达式是当中之中的一个.假设你想了解的 C++11 完整特性, 建议去http://www.open-std.org/看看新标准! 非常多语言都提供了 la ...

  6. SqlHelper简单实现(通过Expression和反射)5.Lambda表达式解析类

    这个ExpressionHelper类,是整个SqlHelper中,最核心的一个类,主要功能就是将Lambda表达式转换为Sql语句.同时这个转换过程比较复杂,所以下面详细讲解一下思路和这个类的作用. ...

  7. python函数,lambda表达式,三目运算,列表解析,递归

    一.自定义函数 定义函数时,函数体不执行:只有在调用函数时,函数体才执行.函数的结构: 1. def 2. 函数名 3. 函数体 def func_name(): 函数体 4. 返回值 如果没有声明返 ...

  8. 解析 Lambda 表达式

    我们先创建一个表达式树: Expression<Func<int, int, int>> expression = (a,b) => a + b; 我们的例子是一个Exp ...

  9. 将Lambda表达式作为参数传递并解析-在构造函数参数列表中使用Lambda表达式

    public class DemoClass { /// <summary> /// 通过Lambda表达式,在构造函数中赋初始值 /// </summary> /// < ...

随机推荐

  1. Python笔试面试题目及答案

    1.is 和==的区别? is:比较的是两个对象的id值是否相等,也就是比较俩对象是否为同一个实例对象.是否指向同一个内存地址 == : 比较的两个对象的内容/值是否相等,默认会调用对象的eq()方法 ...

  2. 利用python自动发邮件

    工作中有时长时间运行代码时需要监控进度,或者需要定期发送固定格式邮件时,可以使用下面定义的邮件函数. 该函数调用了outlook和qqmail的接口,只需要放置到python的环境目录中即可 impo ...

  3. C++线程同步 -- windows

    简介: 在一般情况下,创建一个线程是不能提高程序的执行效率的,所以要创建多个线程.但是多个线程同时运行的时候可能调用线程函数,在多个线程同时对同一个内存地址进行写入, 由于CPU时间调度上的问题,写入 ...

  4. Web Api 接口测试工具:Swagger

    前言:WebApi接口开发完毕后,交付给前端人员或手机端开发者时接口说明文档是必不可少的配套设备,如果公司流程不规范大家使用口口相传的交接方式,而且没有改进的欲望,那你可以到此为止了.Swagger是 ...

  5. Spring MVC使用AOP实现审计日志

    先定一个注解,用于在Controller方法上记录每个方法的用途. package com.zjf.spring.annotation; import java.lang.annotation.Doc ...

  6. safari兼容时间格式 NAN

    问题: safari中不支持2018-08-01这种格式转为时间戳会显示NaN 方案: new Date('2018/08/01').getTime() 将-转化为/ // 正则替换 new Date ...

  7. springboot 配置quart多数据源

    Springboot版本为2.1.6 多数据源配置使用druid进行配置,数据库使用的为Oracle11g,如果使用的是MySQL,直接将数据库的地址和驱动改一下即可 <parent> & ...

  8. pyqt5--动画

    动画类别继承结构图 天子骄龙

  9. layui中从子窗口传递数据到父窗口,第三个子弹层的值传给第二个弹层

    最近做一个项目的需要多个弹层,每个弹层中还需要数据传递, 经过测试,以下方法三个弹层才有效,如果只是有两个弹层,请用其它方法 大概如图,看图自己应该明白 如何在在b页面选择好的值传给a页面的问题,这个 ...

  10. 【leetcode】Valid Palindrome II

    很久没有做题了,今天写个简单难度的练练手感. Given a non-empty string s, you may delete at most one character. Judge wheth ...