Class CollectorUtil
java.lang.Object
cloud.opencode.base.collections.transform.CollectorUtil
CollectorUtil - Custom Collector Utilities
CollectorUtil - 自定义收集器工具
Provides custom collectors for stream operations.
提供用于流操作的自定义收集器。
Features | 主要功能:
- Immutable collection collectors - 不可变集合收集器
- Map collectors - 映射收集器
- Partitioning collectors - 分区收集器
- Special purpose collectors - 特殊用途收集器
Usage Examples | 使用示例:
// Collect to unmodifiable list - 收集为不可修改列表
List<String> list = stream.collect(CollectorUtil.toUnmodifiableList());
// Collect to LinkedHashSet - 收集为 LinkedHashSet
Set<String> set = stream.collect(CollectorUtil.toLinkedHashSet());
// Collect to TreeMap - 收集为 TreeMap
Map<K, V> map = stream.collect(CollectorUtil.toTreeMap(keyFn, valueFn));
Security | 安全性:
- Thread-safe: Yes (stateless utility) - 是(无状态工具)
- Null-safe: No (input must not be null) - 否(输入不能为null)
Performance | 性能特性:
- Time complexity: O(n) accumulation where n is the number of stream elements - 时间复杂度: O(n) 累积,n为流元素数量
- Space complexity: O(n) for collected results - 空间复杂度: O(n),存储收集结果
- Since:
- JDK 25, opencode-base-collections V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptioncounting()Collector to count elements.static Collector<CharSequence, ?, String> joining(CharSequence delimiter) Collector to join strings.static Collector<CharSequence, ?, String> joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix) Collector to join strings with prefix and suffix.static <T,A, R> Collector <T, A, R> of(Supplier<A> supplier, BiConsumer<A, T> accumulator, BinaryOperator<A> combiner, Function<A, R> finisher) Create a custom collector.static <T,R> Collector <T, R, R> of(Supplier<R> supplier, BiConsumer<R, T> accumulator, BinaryOperator<R> combiner) Create a custom collector without finisher.Collector to ArrayList.toArrayList(int capacity) Collector to ArrayList with initial capacity.Collector to HashSet.static <T,K, V> Collector <T, ?, LinkedHashMap<K, V>> toLinkedHashMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) Collector to LinkedHashMap.static <T> Collector<T, ?, LinkedHashSet<T>> Collector to LinkedHashSet.static <T> Collector<T, ?, LinkedList<T>> Collector to LinkedList.static <T, K extends Comparable<? super K>, V>
Collector<T, ?, TreeMap<K, V>> Collector to TreeMap with natural ordering.toTreeMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper, Comparator<? super K> comparator) Collector to TreeMap with custom comparator.static <T extends Comparable<? super T>>
Collector<T, ?, TreeSet<T>> Collector to TreeSet with natural ordering.toTreeSet(Comparator<? super T> comparator) Collector to TreeSet with custom comparator.Collector to unmodifiable list.toUnmodifiableMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) Collector to unmodifiable map.Collector to unmodifiable set.
-
Method Details
-
toArrayList
-
toArrayList
-
toLinkedList
Collector to LinkedList. 收集到 LinkedList。- Type Parameters:
T- element type | 元素类型- Returns:
- collector | 收集器
-
toUnmodifiableList
-
toHashSet
-
toLinkedHashSet
Collector to LinkedHashSet. 收集到 LinkedHashSet。- Type Parameters:
T- element type | 元素类型- Returns:
- collector | 收集器
-
toTreeSet
Collector to TreeSet with natural ordering. 收集到自然排序的 TreeSet。- Type Parameters:
T- element type | 元素类型- Returns:
- collector | 收集器
-
toTreeSet
Collector to TreeSet with custom comparator. 收集到自定义比较器的 TreeSet。- Type Parameters:
T- element type | 元素类型- Parameters:
comparator- the comparator | 比较器- Returns:
- collector | 收集器
-
toUnmodifiableSet
-
toLinkedHashMap
public static <T,K, Collector<T, ?, LinkedHashMap<K,V> V>> toLinkedHashMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) Collector to LinkedHashMap. 收集到 LinkedHashMap。- Type Parameters:
T- element type | 元素类型K- key type | 键类型V- value type | 值类型- Parameters:
keyMapper- the key mapper | 键映射器valueMapper- the value mapper | 值映射器- Returns:
- collector | 收集器
-
toTreeMap
public static <T, K extends Comparable<? super K>, V> Collector<T, ?, TreeMap<K,V>> toTreeMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) Collector to TreeMap with natural ordering. 收集到自然排序的 TreeMap。- Type Parameters:
T- element type | 元素类型K- key type | 键类型V- value type | 值类型- Parameters:
keyMapper- the key mapper | 键映射器valueMapper- the value mapper | 值映射器- Returns:
- collector | 收集器
-
toTreeMap
public static <T,K, Collector<T, ?, TreeMap<K,V> V>> toTreeMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper, Comparator<? super K> comparator) Collector to TreeMap with custom comparator. 收集到自定义比较器的 TreeMap。- Type Parameters:
T- element type | 元素类型K- key type | 键类型V- value type | 值类型- Parameters:
keyMapper- the key mapper | 键映射器valueMapper- the value mapper | 值映射器comparator- the comparator | 比较器- Returns:
- collector | 收集器
-
toUnmodifiableMap
public static <T,K, Collector<T,V> ?, toUnmodifiableMapMap<K, V>> (Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) Collector to unmodifiable map. 收集到不可修改映射。- Type Parameters:
T- element type | 元素类型K- key type | 键类型V- value type | 值类型- Parameters:
keyMapper- the key mapper | 键映射器valueMapper- the value mapper | 值映射器- Returns:
- collector | 收集器
-
counting
-
joining
Collector to join strings. 收集以连接字符串。- Parameters:
delimiter- the delimiter | 分隔符- Returns:
- collector | 收集器
-
joining
public static Collector<CharSequence, ?, String> joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix) Collector to join strings with prefix and suffix. 收集以连接带前缀和后缀的字符串。- Parameters:
delimiter- the delimiter | 分隔符prefix- the prefix | 前缀suffix- the suffix | 后缀- Returns:
- collector | 收集器
-
of
public static <T,A, Collector<T,R> A, ofR> (Supplier<A> supplier, BiConsumer<A, T> accumulator, BinaryOperator<A> combiner, Function<A, R> finisher) Create a custom collector. 创建自定义收集器。- Type Parameters:
T- element type | 元素类型A- accumulator type | 累加器类型R- result type | 结果类型- Parameters:
supplier- the supplier | 供应器accumulator- the accumulator | 累加器combiner- the combiner | 组合器finisher- the finisher | 完成器- Returns:
- collector | 收集器
-
of
public static <T,R> Collector<T,R, ofR> (Supplier<R> supplier, BiConsumer<R, T> accumulator, BinaryOperator<R> combiner) Create a custom collector without finisher. 创建不带完成器的自定义收集器。- Type Parameters:
T- element type | 元素类型R- result type | 结果类型- Parameters:
supplier- the supplier | 供应器accumulator- the accumulator | 累加器combiner- the combiner | 组合器- Returns:
- collector | 收集器
-