Lambda构建 函数式接口编程 功能接口Predicate、Consumer、Function、Supplier使用案例

🍰 个人主页:_小白不加班__
🍞文章有不合理的地方请各位大佬指正。
🍉文章不定期持续更新,如果我的文章对你有帮助➡️ 关注🙏🏻 点赞👍 收藏⭐️

在这里插入图片描述

Lambda表达式的构建

(参数类型 参数名称)->代码体

格式:
(参数类型 参数名称):参数列表
代码体方法体
->箭头分隔参数列表和方法

Lambda举例判断Lambda表达式
  1. ()->"good" ✅没有参数列表,返回值类型为String
  2. ()->{return "good";} ✅没有参数列表,返回值类型为String,显式返回
  3. (Integer i)-> return "good"+i;❌ return显式返回,需要花括号{}
  4. (String s)->{"good";}❌ 返回格式不会,正确为 (String s)->{return "good";}或者(String s)-> "good"

Lambda在哪使用

函数式接口上使用Lambda表达式

函数函数式接口被 @FunctionInterface标记(不被这个注解标记,只有一个方法的接口也可用Lambda)

例如线程

/**
     * 使用Lambda表达式创建并启动一个线程。
     */
    @Test
    void testThread() {
        // 创建一个线程,使用Lambda表达式定义线程的执行逻辑
        Thread t1 = new Thread(() -> {
            System.out.println("t1线程执行");
        });
        // 启动线程
        t1.start();
    }

使用下列构造器:
在这里插入图片描述
方法作为参数:
在这里插入图片描述

官方定义的 Lambda 功能接口

Predicate<T>接受T类型返回boolean结果,常用于判断条件
	@FunctionalInterface
	public interface Predicate<T> {
	    boolean test(T t);
	}

Predicate<T> (T t)-> boolean
代码举例:判断库存是否为0

     @Test
    void streamDemo2() {
        Product product = new Product(4L, "短裤", "短裤", 60.0, 400, "衣服");
        boolean result = checkStockZone((p) -> 0 == p.getStockQuantity(), product);
        System.out.println("product库存是否为0:" + result);
    }
    /**
     * @param t     谓词
     * @param value 要测试的值。
     * @param <T>   泛型参数,表示谓词和值的类型。
     * @return 判断结果
     */
    public static <T> boolean checkStockZone(Predicate<T> t, T value) {
        return t.test(value);
    }

Consumer<T> 接受T类型,无返回值,可用于输出打印。
@FunctionalInterface
public interface MyConsumer<T> {
    void accept(T t);
}

Consumer<T> (T t)->void
代码案例

   /**
     * 使用提供的Consumer函数打印List中的每个元素
     *
     * @param t    一个Consumer接口实例,它定义了一个接受T类型参数的操作。
     * @param list 包含T类型元素的列表。
     * @param <T>  通用类型参数,表示列表中元素的类型。
     */
    public static <T> void printT(Consumer<T> t, List<T> list) {
        for (T valueT : list) {
            t.accept(valueT);
        }
    }
    
    /**
     * Consumer<T> (T t)->void 
     */
    @Test
    void streamDemo3() {
        List<Product> products = new ArrayList<>();
        products.add(new Product(1L, "苹果", "好吃的苹果", 100.0, 100, "水果"));
        products.add(new Product(2L, "香蕉", "好吃的香蕉", 50.0, 200, "水果"));
        products.add(new Product(3L, "短袖", "短袖", 80.0, 300, "衣服"));
        products.add(new Product(4L, "短裤", "短裤", 60.0, 400, "衣服"));
        printT((p) -> System.out.println(p), products);
    }
Supplier<T> ,返回T类型,可用于创建
  • 使用Supplier来实现延迟初始化
  • 在stream中的使用Stream.generate(Supplier<? extends T> s),无限连续流
@FunctionalInterface
public interface Supplier<T> {
    T get();
}

生成随机数集合:

    /**
     * Supplier<T> ()->T
     * 生成随机数集合
     */
    @Test
    void streamDemo4() {
	    //生成随机数 限制10个元素 收集成为集合
        List<Integer> collect = Stream.generate(() -> (int) (Math.random() * 100)).limit(10).collect(Collectors.toList());
        System.out.println(collect);
        // [53, 60, 47, 54, 63, 31, 29, 62, 25, 33]
    }
Function<T,R> 接受一个参数类型为 T 并返回一个结果类型R
  • stream().map(Function<? super T, ? extends R> mapper); 映射
@FunctionalInterface
public interface Function<T, R> {
    R apply(T t);
}

stream().map(Function<? super T, ? extends R> mapper); 映射

   /**
     * Function<T, R>
     * 获取产品集合中 所有id
     */
    @Test
    void streamDemo5() {
        List<Product> products = new ArrayList<>();
        products.add(new Product(1L, "苹果", "好吃的苹果", 100.0, 100, "水果"));
        products.add(new Product(2L, "香蕉", "好吃的香蕉", 50.0, 200, "水果"));
        products.add(new Product(3L, "短袖", "短袖", 80.0, 300, "衣服"));
        List<Long> collect = products.stream().map((p) -> p.getId()).collect(Collectors.toList());
        // [1, 2, 3]
        System.out.println(collect);
    }

根据stream.map自定义映射


    /**
     * Function<T, R>
     */
    @Test
    void streamDemo5() {
        List<Product> products = new ArrayList<>();
        products.add(new Product(1L, "苹果", "好吃的苹果", 100.0, 100, "水果"));
        products.add(new Product(2L, "香蕉", "好吃的香蕉", 50.0, 200, "水果"));
        products.add(new Product(3L, "短袖", "短袖", 80.0, 300, "衣服"));
        List<Long> collect = myMap((p) -> p.getId(), products);
        // [1, 2, 3]
        System.out.println(collect);
    }

    /**
     * 类似于Stream API中的map
     *
     * @param mapper 将列表中的每个元素转换为另一个类型的元素。
     * @param list   要进行映射操作的原始列表。
     * @param <T>    原始列表中元素的类型。
     * @param <R>    映射操作后新列表中元素的类型。
     * @return 包含原始列表中每个元素经过mapper函数转换后的结果的新列表。
     */
    public static <T, R> List<R> myMap(Function<? super T, ? extends R> mapper, List<T> list) {
        List<R> result = new ArrayList<>();
        for (T t : list) {
            result.add(mapper.apply(t));
        }
        return result;
    }

🍉文章不定期持续更新,如果我的文章对你有帮助➡️ 关注🙏🏻 点赞👍 收藏⭐️
转载请注明出处🏀

  • 46
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值