Class OpenCollectors
java.lang.Object
cloud.opencode.base.collections.OpenCollectors
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classFluent collection flow for chained operations. -
Method Summary
Modifier and TypeMethodDescriptionstatic <E> SetAlgebra<E> Create a SetAlgebra for set operations.counting()Collector to count elements (Multiset).static <E> OpenCollectors.CollectorFlow<E> Create a CollectorFlow from an Iterable.static <E> OpenCollectors.CollectorFlow<E> fromStream(Stream<E> stream) Create a CollectorFlow from a Stream.greatestK(int k, Comparator<? super T> comparator) Collector to get greatest K elements.leastK(int k, Comparator<? super T> comparator) Collector to get least K elements.static <E> OpenCollectors.CollectorFlow<E> of(E... elements) Create a CollectorFlow from an array.static <T> Collector<T, ?, T> Collector to get only element.partitionBySize(int size) Collector to partition by size.static <E> Collector<E, ?, ImmutableList<E>> Collector to ImmutableList.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.static <E> Collector<E, ?, ImmutableSet<E>> Collector to ImmutableSet.Collector to Multiset.Collector to get optional single element.
-
Method Details
-
from
Create a CollectorFlow from an Iterable. 从 Iterable 创建 CollectorFlow。- Type Parameters:
E- element type | 元素类型- Parameters:
iterable- the iterable | 可迭代对象- Returns:
- collector flow | 收集器流
-
of
Create a CollectorFlow from an array. 从数组创建 CollectorFlow。- Type Parameters:
E- element type | 元素类型- Parameters:
elements- the elements | 元素- Returns:
- collector flow | 收集器流
-
fromStream
Create a CollectorFlow from a Stream. 从 Stream 创建 CollectorFlow。- Type Parameters:
E- element type | 元素类型- Parameters:
stream- the stream | 流- Returns:
- collector flow | 收集器流
-
algebra
Create a SetAlgebra for set operations. 创建用于集合运算的 SetAlgebra。- Type Parameters:
E- element type | 元素类型- Parameters:
set- the set | 集合- Returns:
- SetAlgebra instance | SetAlgebra 实例
-
toImmutableList
Collector to ImmutableList. 收集到 ImmutableList。- Type Parameters:
E- element type | 元素类型- Returns:
- collector | 收集器
-
toImmutableSet
Collector to ImmutableSet. 收集到 ImmutableSet。- Type Parameters:
E- element type | 元素类型- Returns:
- collector | 收集器
-
toImmutableMap
public static <T,K, Collector<T, ?, ImmutableMap<K,V> 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
-
counting
-
onlyElement
Collector to get only element. 收集唯一元素。- Type Parameters:
T- element type | 元素类型- Returns:
- collector | 收集器
- Throws:
IllegalArgumentException- if not exactly one element | 如果不是恰好一个元素
-
toOptional
Collector to get optional single element. 收集可选的单个元素。- Type Parameters:
T- element type | 元素类型- Returns:
- collector | 收集器
- Throws:
IllegalArgumentException- if more than one element | 如果多于一个元素
-
leastK
Collector to get least K elements. 收集最小的 K 个元素。- Type Parameters:
T- element type | 元素类型- Parameters:
k- number of elements | 元素数量comparator- the comparator | 比较器- Returns:
- collector | 收集器
-
greatestK
Collector to get greatest K elements. 收集最大的 K 个元素。- Type Parameters:
T- element type | 元素类型- Parameters:
k- number of elements | 元素数量comparator- the comparator | 比较器- Returns:
- collector | 收集器
-
partitionBySize
-