Class CollectorUtil

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

public final class CollectorUtil extends Object
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 Details

    • toArrayList

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

      public static <T> Collector<T, ?, ArrayList<T>> toArrayList(int capacity)
      Collector to ArrayList with initial capacity. 收集到带初始容量的 ArrayList。
      Type Parameters:
      T - element type | 元素类型
      Parameters:
      capacity - initial capacity | 初始容量
      Returns:
      collector | 收集器
    • toLinkedList

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

      public static <T> Collector<T,?,List<T>> toUnmodifiableList()
      Collector to unmodifiable list. 收集到不可修改列表。
      Type Parameters:
      T - element type | 元素类型
      Returns:
      collector | 收集器
    • toHashSet

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

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

      public static <T extends Comparable<? super T>> Collector<T, ?, TreeSet<T>> toTreeSet()
      Collector to TreeSet with natural ordering. 收集到自然排序的 TreeSet。
      Type Parameters:
      T - element type | 元素类型
      Returns:
      collector | 收集器
    • toTreeSet

      public static <T> Collector<T, ?, TreeSet<T>> toTreeSet(Comparator<? super T> comparator)
      Collector to TreeSet with custom comparator. 收集到自定义比较器的 TreeSet。
      Type Parameters:
      T - element type | 元素类型
      Parameters:
      comparator - the comparator | 比较器
      Returns:
      collector | 收集器
    • toUnmodifiableSet

      public static <T> Collector<T,?,Set<T>> toUnmodifiableSet()
      Collector to unmodifiable set. 收集到不可修改集合。
      Type Parameters:
      T - element type | 元素类型
      Returns:
      collector | 收集器
    • toLinkedHashMap

      public 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. 收集到 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,V> Collector<T, ?, TreeMap<K,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,V> Collector<T,?,Map<K,V>> toUnmodifiableMap(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

      public static <T> Collector<T,?,Long> counting()
      Collector to count elements. 收集以计数元素。
      Type Parameters:
      T - element type | 元素类型
      Returns:
      collector | 收集器
    • joining

      public static Collector<CharSequence, ?, String> joining(CharSequence delimiter)
      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,R> Collector<T,A,R> of(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,R> of(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 | 收集器