Class OpenCollectors

java.lang.Object
cloud.opencode.base.collections.OpenCollectors

public final class OpenCollectors extends Object
OpenCollectors - Unified Streaming API Entry Point OpenCollectors - 统一流式 API 入口

Provides a fluent API for collection operations, combining the power of streams with collection-specific operations.

提供流式 API 进行集合操作,结合 Stream 的强大功能和集合特定操作。

Features | 主要功能:

  • Fluent collection creation - 流式集合创建
  • Grouping and partitioning - 分组和分区
  • Set algebra operations - 集合代数运算
  • Custom collectors - 自定义收集器

Usage Examples | 使用示例:

// Create from collection - 从集合创建
List<String> names = OpenCollectors.from(users)
    .filter(u -> u.getAge() > 18)
    .map(User::getName)
    .distinct()
    .sorted()
    .limit(10)
    .toList();

// Grouping - 分组
Map<String, List<User>> byCity = OpenCollectors.from(users)
    .groupBy(User::getCity);

// Partitioning - 分区
List<List<User>> batches = OpenCollectors.from(users)
    .partition(100);

Performance | 性能特性:

  • Based on Stream API - 基于 Stream API
  • Lazy evaluation where possible - 尽可能延迟求值

Security | 安全性:

  • Thread-safe: No - 线程安全: 否
  • Null-safe: Yes - 空值安全: 是
Since:
JDK 25, opencode-base-collections V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • from

      public static <E> OpenCollectors.CollectorFlow<E> from(Iterable<E> iterable)
      Create a CollectorFlow from an Iterable. 从 Iterable 创建 CollectorFlow。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      iterable - the iterable | 可迭代对象
      Returns:
      collector flow | 收集器流
    • of

      @SafeVarargs public static <E> OpenCollectors.CollectorFlow<E> of(E... elements)
      Create a CollectorFlow from an array. 从数组创建 CollectorFlow。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      elements - the elements | 元素
      Returns:
      collector flow | 收集器流
    • fromStream

      public static <E> OpenCollectors.CollectorFlow<E> fromStream(Stream<E> stream)
      Create a CollectorFlow from a Stream. 从 Stream 创建 CollectorFlow。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      stream - the stream | 流
      Returns:
      collector flow | 收集器流
    • algebra

      public static <E> SetAlgebra<E> algebra(Set<E> set)
      Create a SetAlgebra for set operations. 创建用于集合运算的 SetAlgebra。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      set - the set | 集合
      Returns:
      SetAlgebra instance | SetAlgebra 实例
    • toImmutableList

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

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

      public static <T,K,V> Collector<T, ?, ImmutableMap<K,V>> toImmutableMap(Function<? super T, ? extends K> keyFunction, Function<? super T, ? extends V> valueFunction)
      Collector to ImmutableMap. 收集到 ImmutableMap。
      Type Parameters:
      T - input element type | 输入元素类型
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      keyFunction - the key function | 键函数
      valueFunction - the value function | 值函数
      Returns:
      collector | 收集器
    • toMultiset

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

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

      public static <T> Collector<T,?,T> onlyElement()
      Collector to get only element. 收集唯一元素。
      Type Parameters:
      T - element type | 元素类型
      Returns:
      collector | 收集器
      Throws:
      IllegalArgumentException - if not exactly one element | 如果不是恰好一个元素
    • toOptional

      public static <T> Collector<T, ?, Optional<T>> toOptional()
      Collector to get optional single element. 收集可选的单个元素。
      Type Parameters:
      T - element type | 元素类型
      Returns:
      collector | 收集器
      Throws:
      IllegalArgumentException - if more than one element | 如果多于一个元素
    • leastK

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

      public static <T> Collector<T,?,List<T>> greatestK(int k, Comparator<? super T> comparator)
      Collector to get greatest 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 - partition size | 分区大小
      Returns:
      collector | 收集器