Stream终止操作

终端操作会从流的流水线生成结果。其结果可以是任何不是流的值,例如:List、Integer,甚至是 void 。流进行了终止操作后,不能再次使用。

Table of Contents

匹配与查找

方法 描述
allMatch(Predicate p) 检查是否匹配所有元素
anyMatch(Predicate p) 检查是否至少匹配一个元素
noneMatch(Predicate p) 检查是否没有匹配所有元素
findFirst() 返回第一个元素
findAny() 返回当前流中的任意元素
count() 返回流中元素总数
max(Comparator c) 返回流中最大值
min(Comparator c) 返回流中最小值
forEach(Consumer c) 内部迭代(使用 Collection 接口需要用户去做迭代,称为外部迭代。相反,Stream API 使用内部迭代——它帮你把迭代做了)
ArrayList<Player> players = T2.getPlayers();

// 检查是否所有球员年龄都超过30岁
var stream = players.stream();

boolean b = stream.allMatch(player -> player.getAge() > 30);
System.out.println(b);

// 返回年龄最大的成员
stream = players.stream();
Optional<Player> max = stream.max(Player::compareTo);
System.out.println(max);

归约

方法 描述
reduce(T iden, BinaryOperator b) 可以将流中元素反复结合起来,得到一个值。返回 T
reduce(BinaryOperator b) 可以将流中元素反复结合起来,得到一个值。返回 Optional
// 学生各科成绩使用数组保存
int[] scores = {78, 67, 87, 57, 90, 86};

// 统计学生的总成绩
IntStream stream = Arrays.stream(scores);
int       sum    = stream.reduce(0, Integer::sum);
System.out.println(sum);

// 有道选择题批改错误,学生的成绩应该加上5分
stream = Arrays.stream(scores);
sum    = stream.reduce(5, Integer::sum);
System.out.println(sum);

上述reduce执行过程是,先执行5+78得到83,然后执行83+67,以此类推。

收集

方法 描述
collect(Collector c) 将流转换为其他形式。接收一个 Collector接口的实现,用于给Stream中元素做汇总的方法

Collector 接口中方法的实现决定了如何对流执行收集的操作(如收集到 List、Set、Map)。另外, Collectors 实用类提供了很多静态方法,可以方便地创建常见收集器实例,下面列出几个常见的:

Modifier and Type Method Description
static <T,C extends Collection<T>>Collector<T,?,C> toCollection(Supplier<C> collectionFactory) Returns a Collector that accumulates the input elements into a new Collection, in encounter order.
static <T> Collector<T,?,Set<T>> toSet() Returns a Collector that accumulates the input elements into a new Set.
static <T> Collector<T,?,List<T>> toList() Returns a Collector that accumulates the input elements into a new List.
static <T,K,U>Collector<T,?,Map<K,U>> toMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper) Returns a Collector that accumulates elements into a Map whose keys and values are the result of applying the provided mapping functions to the input elements.
static <T,K,U>Collector<T,?,Map<K,U>> toMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper, BinaryOperator<U> mergeFunction) Returns a Collector that accumulates elements into a Map whose keys and values are the result of applying the provided mapping functions to the input elements.
static <T,K,U,M extends Map<K,U>>Collector<T,?,M> toMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper, BinaryOperator<U> mergeFunction, Supplier<M> mapFactory) Returns a Collector that accumulates elements into a Map whose keys and values are the result of applying the provided mapping functions to the input elements.
ArrayList<Player> players = T2.getPlayers();

// 收集年龄超过40岁的球员
var stream = players.stream();
List<Player> collect = stream.filter(player -> player.getAge() > 40).collect(Collectors.toList());

collect.forEach(System.out::println);