Springboot3(一、lambda、::的应用)

文章目录

  • 一、使用lambda简化实例创建
    • 1.语法:
    • 2.示例:
    • 3.Function包
      • 3.1 有入参,有返回值【多功能函数】
      • 3.2 有入参,无返回值【消费者】
      • 3.3 无入参,有返回值【提供者】
      • 3.4 无入参,无返回值
  • 二、类::方法的使用
  • 三、综合练习
    • 总结

Spring Boot 支持 Lambda 函数式编程,好处:

  • 简洁性:使用 Lambda 表达式可以大大减少代码的冗余。Lambda 表达式可以将一个功能单元以更简洁的方式传递给方法或函数接口,不需要显式地声明匿名内部类,从而减少了样板代码。
  • 可读性:Lambda 表达式可以使代码更加易读和易理解。通过使用简洁的语法,可以更清晰地表达代码的意图,提高代码的可读性。
  • 函数式编程风格:Lambda 表达式使得函数式编程范式更容易实现。通过使用函数接口(Functional Interface)作为方法的参数类型,可以更方便地使用 Lambda 表达式来处理集合、过滤数据等操作,从而使代码更加简洁和可维护。
  • 并行处理:Lambda 表达式可以很容易地与 Java 8 中引入的并行处理框架结合使用,实现更高效的并行计算。通过并行处理,可以充分利用多核处理器的优势,提高程序的性能。
  • 更好的代码组织:使用 Lambda 表达式可以将代码逻辑更好地组织起来,提高代码的模块化和可重用性。通过将功能单元封装在 Lambda 表达式中,可以将代码逻辑组织得更加紧凑和一致。

一、使用lambda简化实例创建

  • lambda表达式适用于函数式接口
  • 函数式接口:接口中只有一个未实现的接口
  • FunctionalInterface注解可以判断接口是否是函数式接口

1.语法:

(参数) ->  {
	方法体;
	return 返回值;
};

lambda可以根据参数,返回值内容不同,简化程度不同:

  • 无参:() -> {方法体;return 返回值;};
  • 单个参:参数 -> 返回值;
  • 多个参(参数不需要定义类型):(参数,参数……) -> {方法体;return 返回值;};
  • 方法体如果只有一句话,可以省略{}。

2.示例:

package com.example.lambda;

interface SumTest {
    int sum(int i, int j);
}

interface EmptyTest {
    int sum();
}

// FunctionalInterface 检查是否是函数式接口
@FunctionalInterface
interface ManyTest {
    int sum(int i);

    // 当接口中出现多个未实现方法的时候,就不算函数式接口了,使用lambda会报错
    //int add();

    // 实现方法,默认返回1
    default int add(){return 1;}
}

class SumTestImpl implements SumTest {

    @Override
    public int sum(int i, int j) {
        return i * j;
    }

}
/**
 * 使用lambda简化实例创建代码
 * 1.lambda表达式适用于函数式接口
 * 2.函数式接口:接口中只有一个未实现的接口
 * 语法:(参数) -> {方法体;返回值;};
 */
public class Lambda {

    public static void main(String[] args) {
        // 普通方式
        SumTestImpl sum = new SumTestImpl();
        System.out.println(sum.sum(1, 6));
        // 普通方式
        SumTest sum1 = new SumTest() {
            @Override
            public int sum(int i, int j) {
                return i * j;
            }
        };
        System.out.println(sum1.sum(2, 6));

        // lambda方式:方法体可以自定义
        SumTest sumLambda = (int i, int j) -> {
            return i * j;
        };
        System.out.println(sumLambda.sum(3, 6));

        // lambda方式,简化参数类型写法
        SumTest sumLambda1 = (i, j) -> {
            return i * j + i * j;
        };
        System.out.println(sumLambda1.sum(3, 6));

        // 单个参数时,不需要小括号
        ManyTest test = i -> {
            return i+i;
        };
        System.out.println(test.sum(3));

        // 单个参数,最简写法
        test = i -> i+i;
        System.out.println(test.sum(3));

        // 无参时,直接小括号即可
        EmptyTest empty = () -> {
            return 1;
        };
        System.out.println(empty.sum());

    }
}

3.Function包

Springboot3(一、lambda、::的应用)_第1张图片
Function中包含着很多函数式接口供我们使用,下面将例举几个:

  • Function
    R apply(T t):对给定的参数类型 T 进行操作,并返回一个结果类型为 R 的值。
  • BiFunction
    R apply(T t, U u):对给定的参数类型 T 和 U 进行操作,并返回一个结果类型为 R 的值。
  • UnaryOperator(继承自 Function):
    T apply(T t):对给定的参数类型 T 进行操作,并返回一个结果类型仍为 T 的值。
  • BinaryOperator(继承自 BiFunction):
    T apply(T t1, T t2):对给定的参数类型 T 进行操作,并返回一个结果类型仍为 T 的值。
  • Predicate:
    boolean test(T t):对给定的参数类型 T 进行条件判断,返回一个 boolean 值。
  • BiPredicate
    boolean test(T t, U u):对给定的参数类型 T 和 U 进行条件判断,返回一个 boolean 值。
  • Consumer:
    void accept(T t):对给定的参数类型 T 进行处理,没有返回值。
  • BiConsumer
    void accept(T t, U u):对给定的参数类型 T 和 U 进行处理,没有返回值。
  • Supplier:
    T get():获取一个类型为 T 的结果,常用于延迟计算或提供默认值。
  • Runnable:
    void run():执行一些简单的代码块,没有参数和返回值。

3.1 有入参,有返回值【多功能函数】

Function源码如下:

@FunctionalInterface
public interface Function<T, R> {

    /**
     * Applies this function to the given argument.
     *
     * @param t the function argument
     * @return the function result
     */
    R apply(T t);
}

使用示例

Function<Integer, String> function = i -> "使用apply方法,i是入参,此处是返回值"+i;
function.apply(1);

3.2 有入参,无返回值【消费者】

BiConsumer源码如下:

@FunctionalInterface
public interface BiConsumer<T, U> {

    /**
     * Performs this operation on the given arguments.
     *
     * @param t the first input argument
     * @param u the second input argument
     */
    void accept(T t, U u);
}

使用示例

BiConsumer<Integer, String> biConsumer = (c, str) -> System.out.println("使用BiConsumer实现有参,无返回值");
biConsumer.accept(1,"test");

3.3 无入参,有返回值【提供者】

Supplier源码如下:

@FunctionalInterface
public interface Supplier<T> {

    /**
     * Gets a result.
     *
     * @return a result
     */
    T get();
}

使用示例

var list = new ArrayList<Integer>();
list.add(322);
list.add(452);
list.add(621);
list.add(846);
list.add(223);
Collections.sort(list, Integer::compareTo);
Supplier<List> supplier = () -> list;
System.out.println(supplier.get());

3.4 无入参,无返回值

Runnable源码如下:

@FunctionalInterface
public interface Runnable {
    /**
     * When an object implementing interface {@code Runnable} is used
     * to create a thread, starting the thread causes the object's
     * {@code run} method to be called in that separately executing
     * thread.
     * 

* The general contract of the method {@code run} is that it may * take any action whatsoever. * * @see java.lang.Thread#run() */ public abstract void run(); }

使用示例

Runnable runnable = () -> System.out.println("run起来");
runnable.run();

二、类::方法的使用

public static void main(String[] args) {
    var list = new ArrayList<Integer>();
    list.add(322);
    list.add(452);
    list.add(621);
    list.add(846);
    list.add(223);
    Collections.sort(list,(o1, o2) -> o1.compareTo(o2));
    Collections.sort(list, Integer::compareTo);
}

Integer::compareTo == (o1, o2) -> o1.compareTo(o2),就是引用Integer中的compareTo方法进行排序。
此处可以直接使用”::“进行简化。

三、综合练习

判断字符是否为数字,如果是数字就打印出来。

    public static void main(String[] args) {
        // 使用supplier输出一个字符
        Supplier<String> supplier = () -> "111";
        // 使用断言判断是否为数字
        Predicate<String> predicate = str -> str.matches("\\d+");
        // 字符串转数字
        Function<String, Integer> function = Integer::parseInt;
        // 消费数据
        Consumer<Integer> co = c -> {
            System.out.println("输入的字符是数字:"+c);
        };
        String str = supplier.get();
        if(predicate.test(str)){
            co.accept(function.apply(str));
        }else{
            System.out.println("输入的字符不是数字");
        }
	}

总结

java util function包下的所有function定义:

  • Consumer: 消费者
  • Supplier:提供者
  • Predicate: 断言

get/test/apply/accept调用的函数方法;

你可能感兴趣的:(springboot3,java,开发语言,spring,boot)