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;
    }
}
View Code

相关文章:

  • 2022-02-16
  • 2022-12-23
  • 2022-12-23
  • 2021-10-09
  • 2021-10-27
猜你喜欢
  • 2021-06-21
  • 2022-12-23
  • 2021-08-08
  • 2021-07-14
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案