Class MoreCollectorUtil
java.lang.Object
cloud.opencode.base.collections.transform.MoreCollectorUtil
MoreCollectorUtil - Additional Collector Utilities
MoreCollectorUtil - 附加收集器工具
Provides additional advanced collectors for stream operations.
提供用于流操作的附加高级收集器。
Features | 主要功能:
- First/last element collectors - 首/末元素收集器
- Min/max by key collectors - 按键最小/最大收集器
- Multimap collectors - 多值映射收集器
- Conditional collectors - 条件收集器
Usage Examples | 使用示例:
// Get first element - 获取第一个元素
Optional<String> first = stream.collect(MoreCollectorUtil.first());
// Get max by key - 按键获取最大值
Optional<Person> oldest = people.stream()
.collect(MoreCollectorUtil.maxBy(Person::getAge));
// Collect to multimap - 收集为多值映射
Map<String, List<Person>> byCity = stream
.collect(MoreCollectorUtil.toMultimap(Person::getCity));
Security | 安全性:
- Thread-safe: Yes (stateless utility) - 是(无状态工具)
- Null-safe: No (input must not be null) - 否(输入不能为null)
Performance | 性能特性:
- Time complexity: O(n log k) for minK/maxK where n is elements and k is result size; O(n) for most others - 时间复杂度: minK/maxK 为 O(n log k),其他大多数操作为 O(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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordMinMax result container. -
Method Summary
Modifier and TypeMethodDescriptionCollector to check if all match predicate.Collector to check if any match predicate.static <T,A, R, RR> Collector <T, A, RR> collectingAndThen(Collector<T, A, R> downstream, Function<R, RR> finisher) Collector that transforms the result.Collector to get distinct count.elementAt(int index) Collector to get element at index.static <T,A, R> Collector <T, ?, R> Collector that filters before collecting.first()Collector to get the first element.static <T,K, V> Collector <T, ?, ImmutableListMultimap<K, V>> flatteningToImmutableListMultimap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends Stream<? extends V>> valuesMapper) Collector to ImmutableListMultimap with flattening.static <T,K, V> Collector <T, ?, ImmutableSetMultimap<K, V>> flatteningToImmutableSetMultimap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends Stream<? extends V>> valuesMapper) Collector to ImmutableSetMultimap with flattening.static <T,R> Collector <T, ?, R> fold(R identity, BiFunction<R, ? super T, R> folder) Collector to fold elements.static <T,K> Collector <T, ?, ImmutableMap<K, T>> Collector to create unique index map.last()Collector to get the last element.static <T,U, A, R> Collector <T, ?, R> Collector that transforms before collecting.static <T, U extends Comparable<? super U>>
Collector<T, ?, Optional<T>> Collector to get maximum by key.maxK(int k, Comparator<? super T> comparator) Collector to get the maximum k elements.static <T, U extends Comparable<? super U>>
Collector<T, ?, Optional<T>> Collector to get minimum by key.minK(int k, Comparator<? super T> comparator) Collector to get the minimum k elements.static <T> Collector<T, ?, MoreCollectorUtil.MinMax<T>> minMax(Comparator<? super T> comparator) Collector to get min and max.Collector to check if none match predicate.static <T> Collector<T, ?, T> Collector to get only element (throws if not exactly one).partitionBySize(int size) Collector to partition by size.partitioningBy(Predicate<? super T> predicate) Collector to partition by predicate.static <T> Collector<T, ?, T> reducing(T identity, BinaryOperator<T> reducer) Collector to reduce with identity.static <T,K, V> Collector <T, ?, ImmutableBiMap<K, V>> toImmutableBiMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) Collector to ImmutableBiMap.static <T> Collector<T, ?, ImmutableList<T>> Collector to ImmutableList.static <T,K, V> Collector <T, ?, ImmutableListMultimap<K, V>> toImmutableListMultimap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) Collector to ImmutableListMultimap.static <T,K, V> Collector <T, ?, ImmutableMap<K, V>> toImmutableMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) Collector to ImmutableMap.static <T,K, V> Collector <T, ?, ImmutableMap<K, V>> toImmutableMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper, BinaryOperator<V> mergeFunction) Collector to ImmutableMap with merge function.static <T> Collector<T, ?, ImmutableMultiset<T>> Collector to ImmutableMultiset.static <T> Collector<T, ?, ImmutableSet<T>> Collector to ImmutableSet.static <T,K, V> Collector <T, ?, ImmutableSetMultimap<K, V>> toImmutableSetMultimap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) Collector to ImmutableSetMultimap.static <T extends Comparable<? super T>>
Collector<T, ?, ImmutableSortedSet<T>> Collector to ImmutableSortedSet using natural ordering.static <T> Collector<T, ?, ImmutableSortedSet<T>> toImmutableSortedSet(Comparator<? super T> comparator) Collector to ImmutableSortedSet.static <T,R, C, V> Collector <T, ?, ImmutableTable<R, C, V>> toImmutableTable(Function<? super T, ? extends R> rowMapper, Function<? super T, ? extends C> columnMapper, Function<? super T, ? extends V> valueMapper) Collector to ImmutableTable.static <T,R, C, V> Collector <T, ?, ImmutableTable<R, C, V>> toImmutableTable(Function<? super T, ? extends R> rowMapper, Function<? super T, ? extends C> columnMapper, Function<? super T, ? extends V> valueMapper, BinaryOperator<V> mergeFunction) Collector to ImmutableTable with merge function.toMultimap(Function<? super T, ? extends K> keyMapper) Collector to multimap (key to list of values).toMultimap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) Collector to multimap with value transformation.toMultimapSet(Function<? super T, ? extends K> keyMapper) Collector to multimap with set values.Collector to Optional (returns empty if empty, value if single, throws if multiple).
-
Method Details
-
first
-
last
-
onlyElement
Collector to get only element (throws if not exactly one). 收集以获取唯一元素(如果不是恰好一个则抛出异常)。- Type Parameters:
T- element type | 元素类型- Returns:
- collector | 收集器
-
elementAt
-
minBy
public static <T, U extends Comparable<? super U>> Collector<T, ?, Optional<T>> minBy(Function<? super T, ? extends U> keyMapper) Collector to get minimum by key. 收集以按键获取最小值。- Type Parameters:
T- element type | 元素类型U- key type | 键类型- Parameters:
keyMapper- the key mapper | 键映射器- Returns:
- collector | 收集器
-
maxBy
public static <T, U extends Comparable<? super U>> Collector<T, ?, Optional<T>> maxBy(Function<? super T, ? extends U> keyMapper) Collector to get maximum by key. 收集以按键获取最大值。- Type Parameters:
T- element type | 元素类型U- key type | 键类型- Parameters:
keyMapper- the key mapper | 键映射器- Returns:
- collector | 收集器
-
minMax
public static <T> Collector<T, ?, MoreCollectorUtil.MinMax<T>> minMax(Comparator<? super T> comparator) Collector to get min and max. 收集以获取最小值和最大值。- Type Parameters:
T- element type | 元素类型- Parameters:
comparator- the comparator | 比较器- Returns:
- collector returning MinMax | 返回 MinMax 的收集器
-
toMultimap
public static <T,K> Collector<T, ?, Map<K,List<T>>> toMultimap(Function<? super T, ? extends K> keyMapper) Collector to multimap (key to list of values). 收集到多值映射(键到值列表)。- Type Parameters:
T- element type | 元素类型K- key type | 键类型- Parameters:
keyMapper- the key mapper | 键映射器- Returns:
- collector | 收集器
-
toMultimap
public static <T,K, Collector<T, ?, Map<K,V> List<V>>> toMultimap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) Collector to multimap with value transformation. 收集到带值转换的多值映射。- Type Parameters:
T- element type | 元素类型K- key type | 键类型V- value type | 值类型- Parameters:
keyMapper- the key mapper | 键映射器valueMapper- the value mapper | 值映射器- Returns:
- collector | 收集器
-
toMultimapSet
-
filtering
public static <T,A, Collector<T,R> ?, filteringR> (Predicate<? super T> predicate, Collector<? super T, A, R> downstream) Collector that filters before collecting. 在收集前过滤的收集器。- Type Parameters:
T- element type | 元素类型A- accumulator type | 累加器类型R- result type | 结果类型- Parameters:
predicate- the predicate | 谓词downstream- the downstream collector | 下游收集器- Returns:
- collector | 收集器
-
mapping
public static <T,U, Collector<T,A, R> ?, mappingR> (Function<? super T, ? extends U> mapper, Collector<? super U, A, R> downstream) Collector that transforms before collecting. 在收集前转换的收集器。- Type Parameters:
T- input type | 输入类型U- mapped type | 映射类型A- accumulator type | 累加器类型R- result type | 结果类型- Parameters:
mapper- the mapper | 映射器downstream- the downstream collector | 下游收集器- Returns:
- collector | 收集器
-
collectingAndThen
public static <T,A, Collector<T,R, RR> A, collectingAndThenRR> (Collector<T, A, R> downstream, Function<R, RR> finisher) Collector that transforms the result. 转换结果的收集器。- Type Parameters:
T- element type | 元素类型A- accumulator type | 累加器类型R- result type | 结果类型RR- final result type | 最终结果类型- Parameters:
downstream- the downstream collector | 下游收集器finisher- the finisher | 完成器- Returns:
- collector | 收集器
-
fold
Collector to fold elements. 折叠元素的收集器。- Type Parameters:
T- element type | 元素类型R- result type | 结果类型- Parameters:
identity- the identity value | 恒等值folder- the folder function | 折叠函数- Returns:
- collector | 收集器
-
reducing
Collector to reduce with identity. 带恒等值的归约收集器。- Type Parameters:
T- element type | 元素类型- Parameters:
identity- the identity value | 恒等值reducer- the reducer | 归约器- Returns:
- collector | 收集器
-
distinctCount
-
allMatch
-
anyMatch
-
noneMatch
-
toOptional
-
toImmutableList
Collector to ImmutableList. 收集到 ImmutableList。- Type Parameters:
T- element type | 元素类型- Returns:
- collector | 收集器
-
toImmutableSet
Collector to ImmutableSet. 收集到 ImmutableSet。- Type Parameters:
T- element type | 元素类型- Returns:
- collector | 收集器
-
toImmutableSortedSet
public static <T extends Comparable<? super T>> Collector<T, ?, ImmutableSortedSet<T>> toImmutableSortedSet()Collector to ImmutableSortedSet using natural ordering. 使用自然排序收集到 ImmutableSortedSet。- Type Parameters:
T- element type (must be Comparable) | 元素类型(必须是 Comparable)- Returns:
- collector | 收集器
-
toImmutableSortedSet
public static <T> Collector<T, ?, ImmutableSortedSet<T>> toImmutableSortedSet(Comparator<? super T> comparator) Collector to ImmutableSortedSet. 收集到 ImmutableSortedSet。- Type Parameters:
T- element type | 元素类型- Parameters:
comparator- the comparator | 比较器- Returns:
- collector | 收集器
-
toImmutableMap
public static <T,K, Collector<T, ?, ImmutableMap<K,V> V>> toImmutableMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) Collector to ImmutableMap. 收集到 ImmutableMap。- Type Parameters:
T- element type | 元素类型K- key type | 键类型V- value type | 值类型- Parameters:
keyMapper- the key mapper | 键映射器valueMapper- the value mapper | 值映射器- Returns:
- collector | 收集器
-
toImmutableMap
public static <T,K, Collector<T, ?, ImmutableMap<K,V> V>> toImmutableMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper, BinaryOperator<V> mergeFunction) Collector to ImmutableMap with merge function. 收集到带合并函数的 ImmutableMap。- Type Parameters:
T- element type | 元素类型K- key type | 键类型V- value type | 值类型- Parameters:
keyMapper- the key mapper | 键映射器valueMapper- the value mapper | 值映射器mergeFunction- the merge function | 合并函数- Returns:
- collector | 收集器
-
toImmutableBiMap
public static <T,K, Collector<T, ?, ImmutableBiMap<K,V> V>> toImmutableBiMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) Collector to ImmutableBiMap. 收集到 ImmutableBiMap。- Type Parameters:
T- element type | 元素类型K- key type | 键类型V- value type | 值类型- Parameters:
keyMapper- the key mapper | 键映射器valueMapper- the value mapper | 值映射器- Returns:
- collector | 收集器
-
toImmutableMultiset
Collector to ImmutableMultiset. 收集到 ImmutableMultiset。- Type Parameters:
T- element type | 元素类型- Returns:
- collector | 收集器
-
toImmutableListMultimap
public static <T,K, Collector<T, ?, ImmutableListMultimap<K,V> V>> toImmutableListMultimap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) Collector to ImmutableListMultimap. 收集到 ImmutableListMultimap。- Type Parameters:
T- element type | 元素类型K- key type | 键类型V- value type | 值类型- Parameters:
keyMapper- the key mapper | 键映射器valueMapper- the value mapper | 值映射器- Returns:
- collector | 收集器
-
toImmutableSetMultimap
public static <T,K, Collector<T, ?, ImmutableSetMultimap<K,V> V>> toImmutableSetMultimap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) Collector to ImmutableSetMultimap. 收集到 ImmutableSetMultimap。- Type Parameters:
T- element type | 元素类型K- key type | 键类型V- value type | 值类型- Parameters:
keyMapper- the key mapper | 键映射器valueMapper- the value mapper | 值映射器- Returns:
- collector | 收集器
-
flatteningToImmutableListMultimap
public static <T,K, Collector<T, ?, ImmutableListMultimap<K,V> V>> flatteningToImmutableListMultimap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends Stream<? extends V>> valuesMapper) Collector to ImmutableListMultimap with flattening. 收集到带扁平化的 ImmutableListMultimap。- Type Parameters:
T- element type | 元素类型K- key type | 键类型V- value type | 值类型- Parameters:
keyMapper- the key mapper | 键映射器valuesMapper- the values stream mapper | 值流映射器- Returns:
- collector | 收集器
-
flatteningToImmutableSetMultimap
public static <T,K, Collector<T, ?, ImmutableSetMultimap<K,V> V>> flatteningToImmutableSetMultimap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends Stream<? extends V>> valuesMapper) Collector to ImmutableSetMultimap with flattening. 收集到带扁平化的 ImmutableSetMultimap。- Type Parameters:
T- element type | 元素类型K- key type | 键类型V- value type | 值类型- Parameters:
keyMapper- the key mapper | 键映射器valuesMapper- the values stream mapper | 值流映射器- Returns:
- collector | 收集器
-
toImmutableTable
public static <T,R, Collector<T, ?, ImmutableTable<R,C, V> C, toImmutableTableV>> (Function<? super T, ? extends R> rowMapper, Function<? super T, ? extends C> columnMapper, Function<? super T, ? extends V> valueMapper) Collector to ImmutableTable. 收集到 ImmutableTable。- Type Parameters:
T- element type | 元素类型R- row type | 行类型C- column type | 列类型V- value type | 值类型- Parameters:
rowMapper- the row mapper | 行映射器columnMapper- the column mapper | 列映射器valueMapper- the value mapper | 值映射器- Returns:
- collector | 收集器
-
toImmutableTable
public static <T,R, Collector<T, ?, ImmutableTable<R,C, V> C, toImmutableTableV>> (Function<? super T, ? extends R> rowMapper, Function<? super T, ? extends C> columnMapper, Function<? super T, ? extends V> valueMapper, BinaryOperator<V> mergeFunction) Collector to ImmutableTable with merge function. 收集到带合并函数的 ImmutableTable。- Type Parameters:
T- element type | 元素类型R- row type | 行类型C- column type | 列类型V- value type | 值类型- Parameters:
rowMapper- the row mapper | 行映射器columnMapper- the column mapper | 列映射器valueMapper- the value mapper | 值映射器mergeFunction- the merge function | 合并函数- Returns:
- collector | 收集器
-
indexing
public static <T,K> Collector<T, ?, ImmutableMap<K,T>> indexing(Function<? super T, ? extends K> keyMapper) Collector to create unique index map. 收集到唯一索引映射。- Type Parameters:
T- element type | 元素类型K- key type | 键类型- Parameters:
keyMapper- the key mapper | 键映射器- Returns:
- collector | 收集器
-
minK
Collector to get the minimum k elements. 收集以获取最小的 k 个元素。- Type Parameters:
T- element type | 元素类型- Parameters:
k- number of elements | 元素数量comparator- the comparator | 比较器- Returns:
- collector | 收集器
-
maxK
Collector to get the maximum k elements. 收集以获取最大的 k 个元素。- Type Parameters:
T- element type | 元素类型- Parameters:
k- number of elements | 元素数量comparator- the comparator | 比较器- Returns:
- collector | 收集器
-
partitionBySize
-
partitioningBy
public static <T> Collector<T, ?, Map<Boolean,List<T>>> partitioningBy(Predicate<? super T> predicate) Collector to partition by predicate. 按谓词分区的收集器。- Type Parameters:
T- element type | 元素类型- Parameters:
predicate- the predicate | 谓词- Returns:
- collector returning Map with Boolean keys | 返回 Boolean 键映射的收集器
-