lambda表达式与函数式编程

  • 2019 年 10 月 7 日
  • 筆記

01—引

lambda表达式是java支持函数式编程的实现方案,很多高级语言已经支持lambda表达式,像python、javascript等。lambda表达式使代码更加简洁,而且在理解了语法之后,可读性也更加好。

02—语法

无参数

// 1. 无参数  // 1.8版本之前 写法  new Thread(new Runnable() {      @Override      public void run() {          System.out.println("Hello world !");      }  }).start();    // lambda 写法  new Thread(() -> System.out.println("Hello world !")).start();

接受一个参数

  • value省略了类型
  • 省略了参数的小括号
  • 也省略了函数体的大括号
  • forEach 也行函数式编程的写法
List<String> list = Arrays.asList("one", "two", "three");  // 原写法  for(String value : list){      System.out.println(value)  }  // lambda 写法  list.forEach(value -> System.out.println(value));    // 函数式编程写法  list.forEach(System.out::println);

接受两个参数

// 原写法  list.sort(new Comparator<String>() {      @Override      public int compare(String o1, String o2) {          return o1.compareTo(o2);      }  });  // lambda 写法  list.sort((String o1, String o2) -> (o1.compareTo(o2)));  // 函数式编程写法  list.sort(String::compareTo);

方法引用

objectName::instanceMethod  ClassName::staticMethod  ClassName::instanceMethod

构造器引用

ClassName::new

使用场景

  • 匿名类
  • 线程
  • 比较器
  • 回调
  • 集合操作
    • 类似python的集合操作
    • 和stream组合
List<Integer> list = Arrays.asList(4, 7, 9, 12, 0, 18, 8, 10, 3);  // 取最小值  Integer minValue = list.stream().min((Integer o1, Integer o2) -> (o1.compareTo(o2))).get();  // 排序 取前5位  List<Integer> topFive = list.stream().sorted((Integer o1, Integer o2) -> (o1 - o2)).limit(5).collect(Collectors.toList());  // 打印 大于 10的数  list.stream().filter((v) -> v > 10).forEach(System.out::print);

02—函数式接口 包java.util.function下提供很多函数式接口:

Function 输入参数为类型T, 输出为类型R, 记作 T -> R;

// 打印元素的平方  List<Integer> list = Arrays.asList(4, 7, 9, 12, 0, 18, 8, 10, 3);  Function<Integer, Integer> square = x -> x * x; // 平方  list.forEach(x -> System.out.println(square));

Consumer 输入参数为类型T, 输出为void, 记作 T -> void;

// 打印元素  List<Integer> list = Arrays.asList(4, 7, 9, 12, 0, 18, 8, 10, 3);  Consumer<Integer> print = System.out::println; // 打印操作  list.forEach(print);

Supplier 没有输入参数, 输出为类型T, 记作 void -> T;

List<Integer> list = Arrays.asList(4, 7, 9, 12, 0, 18, 8, 10, 3);  Supplier<Integer> instance = () -> (1); // 固定值 1  list.add(instance.get());

Predicate 输入参数为类型T, 输出为类型boolean, 记作 T -> boolean

List<Integer> list = Arrays.asList(4, 7, 9, 12, 0, 18, 8, 10, 3);  Predicate<Integer> isEven = x -> x % 2 == 0; // 是否偶数  list.forEach(isEven::test);

还包括二元函数式接口、其他函数式接口,基本上平时编程足够使用,另外也可以自定义函数式接口:

  • @FunctionalInterface 非必须加的注解;
  • 接口中只能包含一个方法;
@FunctionalInterface  public interface IHandler {      void execute(String v);  }    public class Lambdas {     // 自定义函数式接口      private void customize() {          // 匿名类写法          IHandler handler = new IHandler() {              @Override              public void execute(String v) {                  System.out.println(v);              }          };          handler.execute("匿名类写法");            // lambda 写法          IHandler handler_lambda = System.out::println;          handler_lambda.execute("lambda写法");            // 方法引用          IHandler handler_functional = this::execute;          handler_functional.execute("方法引用写法写法");      }        private void execute(String v) {          System.out.println(v);      }  }

1、https://blog.csdn.net/xinghuo0007/article/details/78607166

2、https://blog.csdn.net/xinghuo0007/article/details/78603908