Class MoreCollectorUtil

java.lang.Object
cloud.opencode.base.collections.transform.MoreCollectorUtil

public final class MoreCollectorUtil extends Object
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:
  • Method Details

    • first

      public static <T> Collector<T, ?, Optional<T>> first()
      Collector to get the first element. 收集以获取第一个元素。
      Type Parameters:
      T - element type | 元素类型
      Returns:
      collector | 收集器
    • last

      public static <T> Collector<T, ?, Optional<T>> last()
      Collector to get the last element. 收集以获取最后一个元素。
      Type Parameters:
      T - element type | 元素类型
      Returns:
      collector | 收集器
    • onlyElement

      public static <T> Collector<T,?,T> onlyElement()
      Collector to get only element (throws if not exactly one). 收集以获取唯一元素(如果不是恰好一个则抛出异常)。
      Type Parameters:
      T - element type | 元素类型
      Returns:
      collector | 收集器
    • elementAt

      public static <T> Collector<T, ?, Optional<T>> elementAt(int index)
      Collector to get element at index. 收集以获取指定索引的元素。
      Type Parameters:
      T - element type | 元素类型
      Parameters:
      index - the index | 索引
      Returns:
      collector | 收集器
    • 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,V> Collector<T, ?, Map<K,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

      public static <T,K> Collector<T, ?, Map<K,Set<T>>> toMultimapSet(Function<? super T, ? extends K> keyMapper)
      Collector to multimap with set values. 收集到带集合值的多值映射。
      Type Parameters:
      T - element type | 元素类型
      K - key type | 键类型
      Parameters:
      keyMapper - the key mapper | 键映射器
      Returns:
      collector | 收集器
    • filtering

      public static <T,A,R> Collector<T,?,R> filtering(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,A,R> Collector<T,?,R> mapping(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,R,RR> Collector<T,A,RR> collectingAndThen(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

      public static <T,R> Collector<T,?,R> fold(R identity, BiFunction<R, ? super T, R> folder)
      Collector to fold elements. 折叠元素的收集器。
      Type Parameters:
      T - element type | 元素类型
      R - result type | 结果类型
      Parameters:
      identity - the identity value | 恒等值
      folder - the folder function | 折叠函数
      Returns:
      collector | 收集器
    • reducing

      public static <T> Collector<T,?,T> reducing(T identity, BinaryOperator<T> reducer)
      Collector to reduce with identity. 带恒等值的归约收集器。
      Type Parameters:
      T - element type | 元素类型
      Parameters:
      identity - the identity value | 恒等值
      reducer - the reducer | 归约器
      Returns:
      collector | 收集器
    • distinctCount

      public static <T> Collector<T,?,Long> distinctCount()
      Collector to get distinct count. 获取不同计数的收集器。
      Type Parameters:
      T - element type | 元素类型
      Returns:
      collector | 收集器
    • allMatch

      public static <T> Collector<T,?,Boolean> allMatch(Predicate<? super T> predicate)
      Collector to check if all match predicate. 检查是否所有元素都匹配谓词的收集器。
      Type Parameters:
      T - element type | 元素类型
      Parameters:
      predicate - the predicate | 谓词
      Returns:
      collector | 收集器
    • anyMatch

      public static <T> Collector<T,?,Boolean> anyMatch(Predicate<? super T> predicate)
      Collector to check if any match predicate. 检查是否有元素匹配谓词的收集器。
      Type Parameters:
      T - element type | 元素类型
      Parameters:
      predicate - the predicate | 谓词
      Returns:
      collector | 收集器
    • noneMatch

      public static <T> Collector<T,?,Boolean> noneMatch(Predicate<? super T> predicate)
      Collector to check if none match predicate. 检查是否没有元素匹配谓词的收集器。
      Type Parameters:
      T - element type | 元素类型
      Parameters:
      predicate - the predicate | 谓词
      Returns:
      collector | 收集器
    • toOptional

      public static <T> Collector<T, ?, Optional<T>> toOptional()
      Collector to Optional (returns empty if empty, value if single, throws if multiple). 收集到 Optional(空则返回空,单个则返回值,多个则抛出异常)。
      Type Parameters:
      T - element type | 元素类型
      Returns:
      collector | 收集器
    • toImmutableList

      public static <T> Collector<T, ?, ImmutableList<T>> toImmutableList()
      Collector to ImmutableList. 收集到 ImmutableList。
      Type Parameters:
      T - element type | 元素类型
      Returns:
      collector | 收集器
    • toImmutableSet

      public static <T> Collector<T, ?, ImmutableSet<T>> 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,V> Collector<T, ?, ImmutableMap<K,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,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. 收集到带合并函数的 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,V> Collector<T, ?, ImmutableBiMap<K,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

      public static <T> Collector<T, ?, ImmutableMultiset<T>> toImmutableMultiset()
      Collector to ImmutableMultiset. 收集到 ImmutableMultiset。
      Type Parameters:
      T - element type | 元素类型
      Returns:
      collector | 收集器
    • toImmutableListMultimap

      public 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. 收集到 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,V> Collector<T, ?, ImmutableSetMultimap<K,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,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. 收集到带扁平化的 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,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. 收集到带扁平化的 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,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. 收集到 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,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. 收集到带合并函数的 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

      public static <T> Collector<T,?,List<T>> minK(int k, Comparator<? super T> comparator)
      Collector to get the minimum k elements. 收集以获取最小的 k 个元素。
      Type Parameters:
      T - element type | 元素类型
      Parameters:
      k - number of elements | 元素数量
      comparator - the comparator | 比较器
      Returns:
      collector | 收集器
    • maxK

      public static <T> Collector<T,?,List<T>> maxK(int k, Comparator<? super T> comparator)
      Collector to get the maximum k elements. 收集以获取最大的 k 个元素。
      Type Parameters:
      T - element type | 元素类型
      Parameters:
      k - number of elements | 元素数量
      comparator - the comparator | 比较器
      Returns:
      collector | 收集器
    • partitionBySize

      public static <T> Collector<T, ?, List<List<T>>> partitionBySize(int size)
      Collector to partition by size. 按大小分区的收集器。
      Type Parameters:
      T - element type | 元素类型
      Parameters:
      size - the partition size | 分区大小
      Returns:
      collector | 收集器
    • 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 键映射的收集器