Java 8 Collectors.collectingAndThen()
JDK源码#
java.util.stream.Collectors#collectingAndThen方法的作用是将Collector的结果在执行一个额外的finisher转换操作,其源码如下:
复制/**
* Adapts a {@code Collector} to perform an additional finishing
* transformation. For example, one could adapt the {@link #toList()}
* collector to always produce an immutable list with:
* <pre>{@code
* List<String> people
* = people.stream().collect(collectingAndThen(toList(), Collections::unmodifiableList));
* }</pre>
*
* @param <T> the type of the input elements
* @param <A> intermediate accumulation type of the downstream collector
* @param <R> result type of the downstream collector
* @param <RR> result type of the resulting collector
* @param downstream a collector
* @param finisher a function to be applied to the final result of the downstream collector
* @return a collector which performs the action of the downstream collector,
* followed by an additional finishing step
*/
public static<T, A, R, RR> Collector<T, A, RR> collectingAndThen(Collector<T, A, R> downstream, Function<R, RR> finisher) {
Set<Collector.Characteristics> characteristics = downstream.characteristics();
if (characteristics.contains(Collector.Characteristics.IDENTITY_FINISH)) {
if (characteristics.size() == 1) {
characteristics = Collectors.CH_NOID;
} else {
characteristics = EnumSet.copyOf(characteristics);
characteristics.remove(Collector.Characteristics.IDENTITY_FINISH);
characteristics = Collections.unmodifiableSet(characteristics);
}
}
return new CollectorImpl<>(downstream.supplier(),
downstream.accumulator(),
downstream.combiner(),
downstream.finisher().andThen(finisher),
characteristics);
}
- T:输入元素的类型
- A:下游Collector的中间堆积类型
- R:下游Collector的结果类型
- RR:结果Collector的结果类型
参数:此方法接受下面列出的两个参数
- downstream: Collector的一个实例,可以使用任何Collector
- finisher: 类型是Function,该函数将应用于下游Collector的最终结果
返回值:返回一个执行下游Collector动作的Collector,然后在finisher函数的帮助下执行附加的转换步骤。
范例:#
创建不可变集合#
// Create an Immutable List
List<String> lt = Stream.of("GEEKS", "For", "GEEKS").collect(Collectors.collectingAndThen(Collectors.toList(), Collections::<String>unmodifiableList));
// Create an Immutable Set
Set<String> st = Stream.of("GEEKS", "FOR", "GEEKS").collect(Collectors.collectingAndThen(Collectors.toSet(), Collections::<String>unmodifiableSet));
// Create an Immutable Map
Map<String, String> mp = Stream.of(new String[][] {{ "1", "Geeks" }, { "2", "For" }, { "3", "Geeks" }})
.collect(Collectors.collectingAndThen(Collectors.toMap(p -> p[0], p -> p[1]), Collections::<String, String>unmodifiableMap));
对象去重:#
List<Xxx> distinctList = rowList.stream()
.collect(Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(obj -> String.join("-", obj.getA(), obj.getB(), obj.getC())))),
ArrayList::new
));
Map<Integer, BigDecimal> mongthValuePair = map.computeIfAbsent(year, ArrayList::new)
.stream().collect(Collectors.toMap(A::getMonth, A::getValue));
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
2017-08-09 Zookeeper系统设计的优点
2017-08-09 分布式系统的基础算法知识[转]