lambda 和 Predicate 的妙用示例

1. 过滤集合数据的多种常用方法

public class DemoTest1 {
    public static void main(String[] args) {
        List<String> languages = Arrays.asList("Java", "Python", "C++", "Lisp", "Haskell", "Scala");

		// 值为 “J” 开头
		languages.stream().filter((str)->str.startsWith("J")).forEach(System.out::println); 

		// 值为 “a” 结尾
		languages.stream().filter((str)->str.endsWith("a")).forEach(System.out::println);   
		
		// 输出全部
		languages.stream().filter((str)->true).forEach(System.out::println);  

		// 长度大于4
		languages.stream().filter((str)->str.length()>4).forEach(System.out::println);  
    }
}

2. 多种 Predicate 的过滤条件联合使用

 public class DemoTest2 {
    public static void main(String[] args) {
        List<String> languages = Arrays.asList("Java", "Python", "C++", "Lisp", "Haskell", "Scala");
        Predicate<String> startsWithJ = (n) -> n.startsWith("J");
        Predicate<String> fourLetterLong = (n) -> n.length() == 4;
        languages.stream().filter(startsWithJ.and(fourLetterLong)).forEach(System.out::println);
    }
}

3. 对集合中的所有数据进行相同处理

public class DemoTest3 {
    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(100, 200, 300, 400, 500);
        list.stream().map((cost) -> cost + .12 * cost).forEach(System.out::println);
    }
}

4. 对集合中的所有数据进行相同处理并整合输出

reduce() 方法:同样可以接受 lambda 表达式,然后对所有的值进行合并

public class DemoTest4 {
    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(100, 200, 300, 400, 500);
        Double sum = list.stream().map((cost) -> cost + .12 * cost).reduce(Double::sum).get();
        System.out.println(sum);
    }
}

5. 通过过滤创建一个新的集合

public class DemoTest5 {
    public static void main(String[] args) {
        List<String> fileNames = Arrays.asList("EDMMICabc20230625","bcd","EDMMICdefg20230625","jk");
        Predicate<String> p1 = (n) -> n.startsWith("EDMMIC");
        Predicate<String> p2 = (n) -> n.endsWith("20230625");
        List<String> list = fileNames.stream().filter(p1.and(p2)).collect(Collectors.toList());
        list.forEach(System.out::println);
    }
}

6. 对集合进行去重

public class DemoTest6 {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(9, 10, 3, 4, 7, 3, 4);
        List<Integer> collect = numbers.stream().distinct().collect(Collectors.toList());
        System.out.println(numbers);
        System.out.println(collect);
    }
}

7. 计算集合元素的最大值、最小值、总和以及平均值

public class DemoTest7 {
    public static void main(String[] args) {
        List<Integer> primes = Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19, 23, 29);
        IntSummaryStatistics statistics = primes.stream().mapToInt((x) -> x).summaryStatistics();
        System.out.println(statistics.getMax()); // 最大值
        System.out.println(statistics.getMin()); // 最小值
        System.out.println(statistics.getSum()); // 总和
        System.out.println(statistics.getAverage()); // 平均值
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,下面再给您提供几个Lambda表达式作为Predicate的实现的代码示例。 1. 筛选出字符串长度大于3的元素: ``` List<String> list = Arrays.asList("apple", "banana", "orange", "pear", "grape"); Stream<String> stream = list.stream().filter(s -> s.length() > 3); stream.forEach(System.out::println); ``` 输出结果为: ``` apple banana orange grape ``` 2. 筛选出年龄大于18岁的人: ``` List<Person> list = Arrays.asList( new Person("Tom", 20), new Person("Jerry", 16), new Person("Alice", 22), new Person("Bob", 18), new Person("Mike", 25) ); Stream<Person> stream = list.stream().filter(p -> p.getAge() > 18); stream.forEach(System.out::println); ``` 其中,Person是一个自定义的类,包含姓名和年龄两个属性,以及相应的get方法。输出结果为: ``` Tom, 20 Alice, 22 Mike, 25 ``` 3. 筛选出价格大于1000并且库存大于0的商品: ``` List<Product> list = Arrays.asList( new Product("iPhone", 8000, 100), new Product("iPad", 5000, 0), new Product("MacBook", 12000, 50), new Product("iWatch", 3000, 200), new Product("AirPods", 1200, 0) ); Stream<Product> stream = list.stream().filter(p -> p.getPrice() > 1000 && p.getStock() > 0); stream.forEach(System.out::println); ``` 其中,Product是一个自定义的类,包含商品名称、价格、库存三个属性,以及相应的get方法。输出结果为: ``` iPhone, 8000, 100 MacBook, 12000, 50 iWatch, 3000, 200 ``` 以上就是Lambda表达式作为Predicate的实现的几个示例,希望对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LF3_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值