Stream API 使用filter()方法过滤元素

List adults = people.stream()
.filter(person -> person.getAge() >= 18)
.collect(Collectors.toList());
filter()方法根据Person对象的年龄属性过滤出成年人,并将结果收集到一个新的List中。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 8引入了Stream API,提供了一种新的操作集合的方式,可以更加方便、高效地进行集合数据的处理。Stream API提供了很多方法,包括中间操作方法和终止操作方法。下面是Stream API中常用的方法使用教程: 1. filter(Predicate<T> predicate):过滤集合中不符合条件的元素。 示例: ``` List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); list.stream() .filter(num -> num > 3) .forEach(System.out::println); ``` 输出结果: ``` 4 5 ``` 2. map(Function<T, R> mapper):对集合中的每个元素进行映射操作。 示例: ``` List<String> list = Arrays.asList("apple", "banana", "orange"); list.stream() .map(str -> str.toUpperCase()) .forEach(System.out::println); ``` 输出结果: ``` APPLE BANANA ORANGE ``` 3. flatMap(Function<T, Stream<R>> mapper):对集合中的每个元素进行映射操作,并将结果展开成一个新的Stream。 示例: ``` List<List<Integer>> list = Arrays.asList( Arrays.asList(1, 2, 3), Arrays.asList(4, 5, 6), Arrays.asList(7, 8, 9) ); list.stream() .flatMap(Collection::stream) .forEach(System.out::println); ``` 输出结果: ``` 1 2 3 4 5 6 7 8 9 ``` 4. distinct():去重。 示例: ``` List<Integer> list = Arrays.asList(1, 2, 2, 3, 3, 3); list.stream() .distinct() .forEach(System.out::println); ``` 输出结果: ``` 1 2 3 ``` 5. sorted():对集合进行排序。 示例: ``` List<Integer> list = Arrays.asList(3, 1, 4, 2, 5); list.stream() .sorted() .forEach(System.out::println); ``` 输出结果: ``` 1 2 3 4 5 ``` 6. limit(long maxSize):限制集合中元素的数量。 示例: ``` List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); list.stream() .limit(3) .forEach(System.out::println); ``` 输出结果: ``` 1 2 3 ``` 7. skip(long n):跳过集合中的前n个元素。 示例: ``` List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); list.stream() .skip(2) .forEach(System.out::println); ``` 输出结果: ``` 3 4 5 ``` 8. forEach(Consumer<T> action):对集合中的每个元素执行指定的操作。 示例: ``` List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); list.stream() .forEach(num -> System.out.println(num * 2)); ``` 输出结果: ``` 2 4 6 8 10 ``` 9. collect(Collector<T, A, R> collector):将Stream中的元素收集到一个集合中。 示例: ``` List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); List<Integer> newList = list.stream() .filter(num -> num > 3) .collect(Collectors.toList()); System.out.println(newList); ``` 输出结果: ``` [4, 5] ``` 10. count():获取Stream元素的数量。 示例: ``` List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); long count = list.stream() .count(); System.out.println(count); ``` 输出结果: ``` 5 ``` 11. anyMatch(Predicate<T> predicate):判断Stream中是否有元素符合条件。 示例: ``` List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); boolean isMatch = list.stream() .anyMatch(num -> num > 3); System.out.println(isMatch); ``` 输出结果: ``` true ``` 12. allMatch(Predicate<T> predicate):判断Stream中所有元素是否都符合条件。 示例: ``` List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); boolean isMatch = list.stream() .allMatch(num -> num > 0); System.out.println(isMatch); ``` 输出结果: ``` true ``` 13. noneMatch(Predicate<T> predicate):判断Stream中是否没有元素符合条件。 示例: ``` List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); boolean isMatch = list.stream() .noneMatch(num -> num < 0); System.out.println(isMatch); ``` 输出结果: ``` true ``` 14. findFirst():获取Stream中的第一个元素。 示例: ``` List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); Optional<Integer> optional = list.stream() .findFirst(); System.out.println(optional.get()); ``` 输出结果: ``` 1 ``` 15. reduce(T identity, BinaryOperator<T> accumulator):将Stream中的元素进行累加。 示例: ``` List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); int sum = list.stream() .reduce(0, (a, b) -> a + b); System.out.println(sum); ``` 输出结果: ``` 15 ``` 以上是Stream API中常用的方法使用教程,可以根据需要进行选择和组合,以实现更加复杂的集合数据处理操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值