Java 8中的收集器collectionAndThen()方法
JavaCollectors类中的collectingAndThen()方法调整收集器以执行额外的完成转换。它返回collector,collector执行下游collector的操作,然后执行附加的结束步骤。
语法如下。
staticCollector collectingAndThen(Collector downstream, Function finisher)
在这里,参数
T-输入元素的类型
A-下游收集器的中间堆积类型
R-下游收集器的结果类型
RR-结果收集器的结果类型
downstream-Collector
finisher-应用于下游收集器最终结果的函数
要使用Java中的Collectors类,请导入以下包。
import java.util.stream.Collectors;
以下是collectingAndThen()在Java中实现方法的示例。
示例
import java.util.List; import java.util.Collections; import java.util.stream.Collectors; import java.util.stream.Stream; public class Demo { public static void main(String[] args) { Listlist = Stream.of("Demo1", "Demo2").collect(Collectors.collectingAndThen( Collectors.toList(), Collections:: unmodifiableList)); System.out.println(list); } }
输出结果
[Demo1, Demo2]