[关闭]
@File 2019-10-15T06:49:34.000000Z 字数 4226 阅读 112

function lambda表达式辅助接口

java


name type description
Consumer Consumer<T> 提供T参数,无返回值
Predicate Predicate<T> 提供T参数,返回bool值
Function Function<T,R> 提供T参数,返回R值
Supplier Supplier<T> 无参,返回T值
UnaryOperator UnaryOperator<T> 提供T参数,返回T值
BiConsumer BiConsumer<T,U> 提供T,U参数,无返回值
BiPredicate BiPredicate<T,U> 提供T,U参数,返回bool值
BiFunction BiFunction<T,U,R> 提供T,U参数,返回R值
BinaryOperator BinaryOperator<T> 提供T,T参数,返回T值

Consumer:T -> void

accept 实现方法

  1. public void run(Consumer<String> consumer) {
  2. // param 是 String 类型的
  3. consumer.accept(param);
  4. }

andThen 重复调用

  1. public void run(Consumer<String> consumer) {
  2. // 等于执行两次 accept
  3. consumer.andThen(param1).accept(param2);
  4. }

Predicate:T -> boolean

test 实现方法

  1. public void run(Predicate<String> predicate) {
  2. // param 是 String 类型的
  3. boolean bool = predicate.test(param);
  4. }

and 多条件判断

  1. public void run(Predicate<String> predicate) {
  2. // param1 && param2
  3. boolean bool = predicate.and(param1).test(param2);
  4. }

or 容错判断

  1. public void run(Predicate<String> predicate) {
  2. // param1 || param2
  3. boolean bool = predicate.or(param1).test(param2);
  4. }

negate 取反判断

  1. public void run(Predicate<String> predicate) {
  2. // !param
  3. boolean bool = predicate.negate().test(param);
  4. }

isEqual 值相同判断

  1. public void run(String param) {
  2. // 和 param.equals("lidaye") 作用一样
  3. boolean bool = Predicate.isEqual(param).test("lidaye");
  4. }

Function:T -> R

apply 实现方法

  1. // 模拟状态码
  2. private static Integer code = 404;
  3. public static void main(String[] args) {
  4. // 调用:设定不同状态码的含义
  5. run(c -> {
  6. String msg = "ojbk";
  7. switch (c){
  8. case 403:
  9. msg = "未授权!";
  10. break;
  11. case 404:
  12. msg = "找不到页面!";
  13. break;
  14. case 500:
  15. msg = "网络异常!";
  16. break;
  17. default:
  18. }
  19. return msg;
  20. });
  21. }
  22. // 封装 lambda 方法
  23. public static String run(Function<Integer,String> function) {
  24. return function.apply(code);
  25. }

compose 先执行逻辑

  1. // 模拟一个数值
  2. private static Integer num = 10;
  3. public static void main(String[] ages){
  4. // 调用传入一个逻辑 function
  5. run(u -> u * 2);
  6. }
  7. // 封装 lambda 方法
  8. public static Integer run(Function<Integer,Integer> function) {
  9. // 先执行 compose() 参数中的逻辑(n * n)
  10. // 再吧 n * n 的结果传给 function 逻辑
  11. return function.compose((Function<Integer, Integer>) n -> n * n).apply(num);
  12. }

andThen 后执行逻辑

  1. // 模拟一个数值
  2. private static Integer num = 10;
  3. public static void main(String[] ages){
  4. // 调用传入一个逻辑 function
  5. run(u -> u * 2);
  6. }
  7. // 封装 lambda 方法
  8. public static Integer run(Function<Integer,Integer> function) {
  9. // 先执行 function 逻辑(n * 2)
  10. // 再吧 n * 2 的结果传给 andThen() 参数中的逻辑
  11. return function.andThen(n -> n * n).apply(num);
  12. }

identity 静态取Function对象

  1. public static void main(String[] ages){
  2. // 调用传入一个值
  3. run(10);
  4. }
  5. // 封装 lambda 方法
  6. public static Integer run(Integer num) {
  7. Integer i = (Integer) Function
  8. // 取 Function 对象
  9. .identity()
  10. // 传入逻辑
  11. .compose((Function<Integer, Integer>) n -> n * n)
  12. // 传入参数值
  13. .apply(num);
  14. // 返回结果
  15. return i;
  16. }

Supplier:() -> T

get 实现方法

  1. public static void main(String[] ages){
  2. // 传入数据,拿到数组
  3. run(() -> {
  4. List<User> users = new ArrayList<>();
  5. users.add(new User("李大爷", 20));
  6. users.add(new User("胡大妈", 18));
  7. return users;
  8. });
  9. }
  10. // 封装 lambda 方法
  11. public static String[] run(Supplier<List<User>> supplier) {
  12. List<User> users = supplier.get();
  13. List<String> list = new ArrayList<>(users.size());
  14. // 遍历取出 name 的值
  15. users.forEach(u -> list.add(u.getName()));
  16. return list.toArray(new String[0]);
  17. }

UnaryOperator:T -> T

  1. // 参数类型和返回类型一样的 Function
  2. UnaryOperator<T> extends Function<T, T>

调用案例

  1. public static void main(String[] ages){
  2. // 传入数据
  3. System.out.println(run(u -> u * u));
  4. }
  5. public static Integer run(UnaryOperator<Integer> unaryOperator) {
  6. return unaryOperator.apply(10);
  7. }

BiConsumer:(T,U) -> void

  1. public void run(BiConsumer<String, Integer> biConsumer) {
  2. // 实现方法时可以接收两个参数
  3. biConsumer.accept(name,age);
  4. }

BiPredicate:(T,U) -> boolean

  1. public void run(BiPredicate<String, Integer> biPredicate) {
  2. // 实现方法时可以接收两个参数
  3. boolean bool = biPredicate.test(name,age);
  4. }

BiFunction:(T,U) -> R

  1. public static String run(BiFunction<Integer, String, String> biFunction) {
  2. return biFunction.apply(code,String);
  3. }

BinaryOperator:(T,T) -> T

minBy 取最小值

  1. public static Integer run(int num1, int num2) {
  2. return BinaryOperator
  3. // 设置Comparator排序规则
  4. .minBy((Comparator<Integer>)(i1,i2) -> i1-i2)
  5. // 传入排序参数
  6. .apply(num1,num2);
  7. }

maxBy 取最大值

  1. public static Integer run(int num1, int num2) {
  2. return BinaryOperator
  3. // 设置Comparator排序规则
  4. .maxBy((Comparator<Integer>)(i1,i2) -> i1-i2)
  5. // 传入排序参数
  6. .apply(num1,num2);
  7. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注