Java 8 Collectors.collectingAndThen()
2021/8/9 22:06:23
本文主要是介绍Java 8 Collectors.collectingAndThen(),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
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 ));
^_^
参考文章:
Java Collectors collectingAndThen()用法及代码示例
这篇关于Java 8 Collectors.collectingAndThen()的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-15鸿蒙生态设备数量超8亿台
- 2024-05-13TiDB + ES:转转业财系统亿级数据存储优化实践
- 2024-05-09“2024鸿蒙零基础快速实战-仿抖音App开发(ArkTS版)”实战课程已上线
- 2024-05-09聊聊如何通过arthas-tunnel-server来远程管理所有需要arthas监控的应用
- 2024-05-09log4j2这么配就对了
- 2024-05-09nginx修改Content-Type
- 2024-05-09Redis多数据源,看这篇就够了
- 2024-05-09Google Chrome驱动程序 124.0.6367.62(正式版本)去哪下载?
- 2024-05-09有没有大佬知道这种数据应该怎么抓取呀?
- 2024-05-09这种运行结果里的10.100000001,怎么能最快改成10.1?