将Java8流分组而不收集它 [英] Grouping Java8 stream without collecting it

查看:679
本文介绍了将Java8流分组而不收集它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java 8中是否有任何方法可以在 java.util.stream.Stream 中对元素进行分组而不收集它们?我希望结果再次是 Stream 。因为我必须使用大量数据甚至是无限流,所以我无法先收集数据并再次流式传输结果。

Is there any way in Java 8 to group the elements in a java.util.stream.Stream without collecting them? I want the result to be a Stream again. Because I have to work with a lot of data or even infinite streams, I cannot collect the data first and stream the result again.

所有需要分组的元素都是在第一个流中连续。因此,我喜欢让流评估变得懒惰。

All elements that need to be grouped are consecutive in the first stream. Therefore I like to keep the stream evaluation lazy.

推荐答案

使用标准Stream API无法做到这一点。一般情况下,您无法执行此操作,因为将来可能会出现属于任何已创建组的新项目,因此在处理完所有输入之前,您无法将组传递给下游分析。

There's no way to do it using standard Stream API. In general you cannot do it as it's always possible that new item will appear in future which belongs to any of already created groups, so you cannot pass your group to downstream analysis until you process all the input.

但是,如果您事先知道要分组的项目在输入流中始终相邻,则可以使用增强Stream API的第三方库来解决您的问题。其中一个库是 StreamEx ,由我免费提供。它包含许多部分缩减运算符,它们根据某些谓词将相邻项目折叠为单个。通常你应该提供一个 BiPredicate 来测试两个相邻的项目,如果它们应该组合在一起,则返回true。部分减少操作列在下面:

However if you know in advance that items to be grouped are always adjacent in input stream, you can solve your problem using third-party libraries enhancing Stream API. One of such libraries is StreamEx which is free and written by me. It contains a number of "partial reduction" operators which collapse adjacent items into single based on some predicate. Usually you should supply a BiPredicate which tests two adjacent items and returns true if they should be grouped together. Some of partial reduction operations are listed below:


  • collapse(BiPredicate) :replace每个组都有该组的第一个元素。例如, collapse(Objects :: equals)对于从流中删除相邻的重复项非常有用。

  • groupRuns(BiPredicate) :将每个组替换为组元素列表(因此 StreamEx< T> 将转换为 StreamEx< ;列表与LT; T>> )。例如, stringStream.groupRuns((a,b) - > a.charAt(0)== b.charAt(0))将创建字符串列表流其中每个列表包含以相同字母开头的相邻字符串。

  • collapse(BiPredicate): replace each group with the first element of the group. For example, collapse(Objects::equals) is useful to remove adjacent duplicates from the stream.
  • groupRuns(BiPredicate): replace each group with the List of group elements (so StreamEx<T> is converted to StreamEx<List<T>>). For example, stringStream.groupRuns((a, b) -> a.charAt(0) == b.charAt(0)) will create stream of Lists of strings where each list contains adjacent strings started with the same letter.

其他部分缩减操作包括 intervalMap runLengths() 等等。

Other partial reduction operations include intervalMap, runLengths() and so on.

所有部分减少操作都是惰性的,并行 - 友好且高效。

All partial reduction operations are lazy, parallel-friendly and quite efficient.

请注意,您可以使用<$从常规Java 8流中轻松构造 StreamEx 对象C $ C> StreamEx.of(流)。还有一些方法可以从数组,Collection,Reader等构建它。 StreamEx 类实现 Stream 接口和100 %与标准Stream API兼容。

Note that you can easily construct a StreamEx object from regular Java 8 stream using StreamEx.of(stream). Also there are methods to construct it from array, Collection, Reader, etc. The StreamEx class implements Stream interface and 100% compatible with standard Stream API.

这篇关于将Java8流分组而不收集它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆