本文共 7457 字,大约阅读时间需要 24 分钟。
Java Stream collect() performs a mutable reduction operation on the elements of the stream. This is a terminal operation.
Java Stream collect()对流的元素执行可变的约简操作。 这是终端操作。
A mutable reduction operation process the stream elements and then accumulate it into a mutable result container. Once the elements are processed, a combining function merges all the result containers to create the result.
可变还原操作处理流元素,然后将其累积到可变结果容器中。 处理完元素后,合并功能将合并所有结果容器以创建结果。
There are two variants of collect() method.
collect()方法有两种变体。
The Collector is an interface that provides a wrapper for the supplier, accumulator, and combiner objects. The second method is useful when we are utilizing the Collectors class to provide built-in Collector implementation.
收集器是一个接口,为供应商,累加器和合并器对象提供包装。 当我们利用Collectors类提供内置的Collector实现时,第二种方法很有用。
The three parameters of the collect() function are:
collect()函数的三个参数是:
Let’s look at some examples of Stream.collect() method.
让我们看一下Stream.collect()方法的一些示例。
Let’s say you want to concatenate the list of strings to create a new string. We can use Stream collect() function to perform a mutable reduction operation and concatenate the list elements.
假设您要连接字符串列表以创建新字符串。 我们可以使用Stream collect()函数执行可变的约简操作并连接列表元素。
Listvowels = List.of("a", "e", "i", "o", "u");// sequential stream - nothing to combineStringBuilder result = vowels.stream().collect(StringBuilder::new, (x, y) -> x.append(y), (a, b) -> a.append(",").append(b));System.out.println(result.toString());// parallel stream - combiner is combining partial resultsStringBuilder result1 = vowels.parallelStream().collect(StringBuilder::new, (x, y) -> x.append(y), (a, b) -> a.append(",").append(b));System.out.println(result1.toString());
Output:
输出:
aeioua,e,i,o,u
If you want to concatenate the list of strings, we can use the method references to reduce the code size.
如果要串联字符串列表,我们可以使用方法引用来减少代码大小。
String result2 = vowels.parallelStream() .collect(StringBuilder::new, StringBuilder::append, StringBuilder::append) .toString();
The Collectors class provides many useful implementations of the Collector interface. Let’s look at an example where we will filter the list of integers to select only even integers. is an intermediate operation and returns a stream. So, we will use the collect() function to create the list from this stream.
Collector类提供了Collector接口的许多有用的实现。 让我们看一个示例,其中我们将过滤整数列表以仅选择偶数整数。 是一个中间操作,返回一个流。 因此,我们将使用collect()函数从此流中创建列表。
Listnumbers = List.of(1, 2, 3, 4, 5, 6); List evenNumbers = numbers.stream().filter(x -> x % 2 == 0).collect(Collectors.toList());System.out.println(evenNumbers); // [2, 4, 6]
The Collectors.toList() returns a Collector implementation that accumulates the input elements into a new List.
Collectors.toList()返回一个Collector实现,该实现将输入元素累积到新的List中。
We can use Collectors.toSet() to collect the stream elements into a new Set.
我们可以使用Collectors.toSet()将流元素收集到一个新的Set中。
Listnumbers = List.of(1, 2, 3, 4, 5, 6);Set oddNumbers = numbers.parallelStream().filter(x -> x % 2 != 0).collect(Collectors.toSet());System.out.println(oddNumbers); // [1, 3, 5]
We can use Collectors.toMap() function to collect the stream elements to a . This method accepts two arguments for mapping key and the corresponding value in the Map.
我们可以使用Collectors.toMap()函数将流元素收集到 。 此方法接受两个用于映射键的参数和Map中的对应值。
Listnumbers = List.of(1, 2, 3, 4, 5, 6);Map mapOddNumbers = numbers.parallelStream().filter(x -> x % 2 != 0) .collect(Collectors.toMap(Function.identity(), x -> String.valueOf(x)));System.out.println(mapOddNumbers); // {1=1, 3=3, 5=5}
We can use Collectors joining() methods to get a Collector that concatenates the input stream CharSequence elements in the encounter order. We can use this to concatenate a stream of strings, , or StringBuilder.
我们可以使用Collectors join()方法来获取Collector,该Collector以遭遇顺序将输入流CharSequence元素串联在一起。 我们可以使用它来连接字符串流, 或StringBuilder。
jshell> String value = Stream.of("a", "b", "c").collect(Collectors.joining());value ==> "abc"jshell> String valueCSV = Stream.of("a", "b", "c").collect(Collectors.joining(","));valueCSV ==> "a,b,c"jshell> String valueCSVLikeArray = Stream.of("a", "b", "c").collect(Collectors.joining(",", "{", "}"));valueCSVLikeArray ==> "{a,b,c}"jshell> String valueObject = Stream.of("1", new StringBuffer("2"), new StringBuilder("3")).collect(Collectors.joining());valueObject ==> "123"
Output:
输出:
Java Stream collect() is mostly used to collect the stream elements to a collection. It’s a terminal operation. It takes care of synchronization when used with a parallel stream. The Collectors class provides a lot of Collector implementation to help us out.
Java Stream collect()通常用于将流元素收集到一个集合中。 这是终端操作。 与并行流一起使用时,它负责同步。 Collectors类提供了许多Collector实现来帮助我们。
翻译自:
转载地址:http://fmqzd.baihongyu.com/