本节是lambda表达式的一个入门课,讲解的同时配有练习demojava
前言什么是lambda表达式?基础语法函数式接口本身实现一个函数式接口jdk提供的函数式接口ConsumersupplierfunctionPredicate其余场景python
lambda表达式是java8推出的一个新特性。标志着从jdk1.8开始,java正式支持函数式编程。使用lambda表达式能够用简洁流畅的代码完成一个功能,这也在必定程度上,能够减轻了java一直为人诟病的代码简洁性问题。web
lambda表达式能够简单理解为一段能够传递的代码,核心思想是将面向对象中传递数据变为传递行为。下面举一个编写线程的例子。sql
1Runnable runnable = new Runnable() {
2 @Override
3 public void run() {
4 System.out.println("hello lambda");
5 }
6};
这里使用的是匿名内部类的方式。匿名内部类实现的也是将代码做为数据传递,不过使用匿名内部类仍是不够简洁,那么使用lambda表达式的代码是什么样的呢?编程
1Runnable runnable = () -> System.out.println("hello lambda");
咱们能够看到使用lambda表达式实现起来很是的简单,只须要一行代码便可,接下来咱们就试着分析一下这段代码。数组
咱们能够看到上述的示例代码,lambda表达式的引用变量是一个runnable类型,事实上,java为咱们提供了多种变量类型供咱们选择,这些类型咱们称之为函数式接口。
函数式接口是只有一个方法的接口,用做lambda表达式的类型。好比咱们以前提到的Runnable接口app
1@FunctionalInterface
2public interface Runnable {
3 /**
4 * When an object implementing interface <code>Runnable</code> is used
5 * to create a thread, starting the thread causes the object's
6 * <code>run</code> method to be called in that separately executing
7 * thread.
8 * <p>
9 * The general contract of the method <code>run</code> is that it may
10 * take any action whatsoever.
11 *
12 * @see java.lang.Thread#run()
13 */
14 public abstract void run();
15}
咱们能够看到interface上一个注解@FunctionInterface,它的做用是告诉编译器这个接口是一个函数式接口。dom
咱们能够本身实现一个函数式接口,判断数据的数据是否合法ide
1/**
2 * 自定义函式数接口
3 *
4 * @author jialin.li
5 * @date 2020-03-18 14:22
6 */
7@FunctionalInterface
8public interface CustomFunctionInterface<T> {
9 boolean legal(T t);
10}
测试方法:函数式编程
1/**
2 * 测试.
3 *
4 * @author jialin.li
5 * @date 2020-03-18 14:23
6 */
7public class Test {
8
9 public boolean legal(int value, CustomFunctionInterface<Integer> functionInterface){
10 return functionInterface.legal(value);
11 }
12
13 public static void main(String[] args) {
14 System.out.println(new Test().legal(-8, (x) -> x >= 0));
15 }
16}
测试结果:
1false
jdk在java.util.function中为咱们提供了很是丰富的函数式接口,大体能够分为4类:
将输入的10进制数,转换为2进制数输出
1public void toBinaryString(int value, Consumer<Integer> consumer){
2 consumer.accept(value);
3}
4
5public static void main(String[] args) {
6 new Test().toBinaryString(15, (x) -> System.out.println(Integer.toBinaryString(x)));
7}
生成n个随机数100之内的随机数,以数组的形式返回
1public int[] getRandomArr(int n, Supplier<Integer> supplier) {
2 int[] arr = new int[n];
3 for (int i = 0; i < n; i++) {
4 arr[i] = supplier.get();
5 }
6 return arr;
7}
8
9public static void main(String[] args) {
10 int[] arr = new Test().getRandomArr(10, () -> new Random().nextInt(100));
11 System.out.println(Arrays.toString(arr));
12}
生成一个[1,n]组成的阶乘数组,例如输入3,返回[1,4,9]
1public int[] getFactorialArr(int n, Function<Integer,Integer> function) {
2 int[] arr = new int[n];
3 for (int i = 1; i <= n; i++) {
4 arr[i - 1] = function.apply(i);
5 }
6 return arr;
7}
8
9public static void main(String[] args) {
10 int[] arr = new Test().getFactorialArr(10, (x) -> x*x);
11 System.out.println(Arrays.toString(arr));
12}
判断一个数是不是偶数
1public boolean isEven(int n, Predicate<Integer> predicate) {
2 return predicate.test(n);
3}
4
5public static void main(String[] args) {
6 boolean isEven = new Test().isEven(1102, (x) -> (x % 2 == 0));
7 System.out.println(isEven);
8}
除了上述的4个接口,java.util.function中还有许多其余的接口,能够实现各类功能。好比实现2个数的加法运算:
1public static void main(String[] args) {
2 BinaryOperator<Integer> binaryOperator = Integer::sum;
3 System.out.println(binaryOperator.apply(1, 3));
4}
实现List遍历:
1public static void main(String[] args) {
2 List<String> list = new ArrayList<>(Arrays.asList("java", "python", "go", "sql"));
3 list.forEach(System.out::println);
4}
更多场景还请您去探索,感觉函数式编程的魅力吧
最后,期待您的订阅和点赞,专栏每周都会更新,但愿能够和您一块儿进步,同时也期待您的批评与指正!