Class OpenStream
java.lang.Object
cloud.opencode.base.core.OpenStream
Stream Utility Class - Enhanced Stream operations with JDK 25 Gatherers support
Stream工具类 - 增强的Stream操作,支持JDK 25 Gatherers
Provides utility methods for working with Java Streams, including creation, transformation, collection, and parallel processing.
提供Java Stream的实用方法,包括创建、转换、收集和并行处理。
Features | 主要功能:
- Stream creation (of, from, range, generate) - Stream创建
- Stream transformation (batch, window, sliding) - Stream转换
- Parallel processing utilities - 并行处理工具
- Collectors extensions - Collectors扩展
- Stream combination (zip, merge, concat) - Stream组合
Usage Examples | 使用示例:
// Batch processing - 批量处理
List<List<User>> batches = OpenStream.batch(users.stream(), 100);
// Sliding window - 滑动窗口
Stream<List<Integer>> windows = OpenStream.slidingWindow(numbers, 3);
// Zip streams - 合并流
Stream<Pair<A, B>> zipped = OpenStream.zip(streamA, streamB, Pair::of);
// Parallel with concurrency limit - 限制并发的并行处理
results = OpenStream.parallelWithLimit(items, 10, item -> process(item));
Security | 安全性:
- Thread-safe: Yes (stateless) - 线程安全: 是 (无状态)
- Null-safe: Yes - 空值安全: 是
- Since:
- JDK 25, opencode-base-core V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordIndexed value record 带索引的值记录 -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> booleanChecks if all elements match 检查是否所有元素都匹配static <T> booleanChecks if any element matches 检查是否有元素匹配static OptionalDoubleaverageInt(IntStream stream) Gets average of integers 获取整数平均值Splits stream into batches of fixed size 将Stream分割成固定大小的批次batchStream(Collection<T> source, int batchSize) Creates a stream of batches 创建批次流static <T> longCounts elements 统计元素数量static <T,K> Stream <T> distinctBy(Stream<T> stream, Function<T, K> keyExtractor) Distinct by key 按键去重static <T> Stream<T> Drops elements while predicate is true 在谓词为真时跳过元素static <T> Stream<T> filterNulls(Stream<T> stream) Filters nulls from stream 从流中过滤空值static <T> Optional<T> Finds first matching element 查找第一个匹配元素static <T> Stream<T> Flattens nested streams 展平嵌套流static <T> Stream<T> flattenCollections(Stream<? extends Collection<T>> stream) Flattens nested collections 展平嵌套集合static <T> Stream<T> Creates a stream from an iterable 从Iterable创建Streamstatic <T> Stream<T> Creates a stream from an iterator 从Iterator创建Streamstatic <T> Stream<T> Creates a stream from an optional 从Optional创建Streamstatic <T> Stream<T> Generates an infinite stream 生成无限StreamGroups by key 按键分组static <T> Stream<T> interleave(Stream<T> streamA, Stream<T> streamB) Interleaves two streams 交错合并两个流static <T> Stream<T> iterate(T seed, Predicate<T> hasNext, UnaryOperator<T> next) Generates a finite stream with seed and predicate 使用种子和谓词生成有限Streamstatic <T> Stream<T> iterate(T seed, UnaryOperator<T> next) Generates an infinite stream with seed 使用种子生成无限Streamstatic <T> Stringjoining(Stream<T> stream, CharSequence delimiter) Joins to string 连接为字符串static <T> Stringjoining(Stream<T> stream, CharSequence delimiter, CharSequence prefix, CharSequence suffix) Joins to string with prefix and suffix 使用前缀和后缀连接为字符串static <T> Stream<T> Limits stream size 限制流大小static <T> Optional<T> max(Stream<T> stream, Comparator<T> comparator) Gets max element 获取最大元素static <T> Stream<T> Merges multiple streams 合并多个流static <T> Optional<T> min(Stream<T> stream, Comparator<T> comparator) Gets min element 获取最小元素static <T> booleanChecks if no elements match 检查是否没有元素匹配static <T> Stream<T> of(T... elements) Creates a stream from varargs 从可变参数创建Streamstatic <T> List<T> parallelFilter(Collection<T> source, Predicate<T> predicate) Filters in parallel 并行过滤static <T,R> List <R> parallelMap(Collection<T> source, int parallelism, Function<T, R> mapper) Processes in parallel with limited concurrency 使用有限并发进行并行处理static <T,R> List <R> parallelMap(Collection<T> source, Function<T, R> mapper) Processes in parallel and collects results 并行处理并收集结果partitionBy(Stream<T> stream, Predicate<T> predicate) Partitions by predicate 按谓词分区static <T> Stream<T> Peeks at each element for debugging 查看每个元素用于调试static IntStreamrange(int startInclusive, int endExclusive) Creates an int range stream 创建整数范围Streamstatic LongStreamrange(long startInclusive, long endExclusive) Creates a long range stream 创建长整数范围Streamstatic IntStreamrangeClosed(int startInclusive, int endInclusive) Creates an int range stream (closed) 创建闭区间整数范围Streamstatic <T> Stream<T> Skips first n elements 跳过前n个元素slidingWindow(Collection<T> source, int windowSize) Creates a sliding window stream 创建滑动窗口流slidingWindow(Collection<T> source, int windowSize, int step) Creates a sliding window stream with custom step 创建自定义步长的滑动窗口流static <T> Stream<T> sorted(Stream<T> stream, Comparator<T> comparator) Sorts stream 排序流static doublesumDouble(DoubleStream stream) Gets sum of doubles 获取双精度数和static intGets sum of integers 获取整数和static longsumLong(LongStream stream) Gets sum of longs 获取长整数和static <T> Stream<T> Takes elements while predicate is true 在谓词为真时获取元素static <T,K> Map <K, T> Collects to map with key extractor 使用键提取器收集为Mapstatic <T,K, V> Map <K, V> Collects to map with key and value extractors 使用键和值提取器收集为Mapstatic <T> List<T> toUnmodifiableList(Stream<T> stream) Collects to unmodifiable list 收集为不可变列表static <T> Set<T> toUnmodifiableSet(Stream<T> stream) Collects to unmodifiable set 收集为不可变集合tumblingWindow(Collection<T> source, int windowSize) Creates a tumbling window stream (non-overlapping) 创建翻滚窗口流(不重叠)static <A,B, R> Stream <R> zip(Stream<A> streamA, Stream<B> streamB, BiFunction<A, B, R> zipper) Zips two streams into pairs 将两个流合并成对static <T> Stream<OpenStream.IndexedValue<T>> zipWithIndex(Stream<T> stream) Zips stream with indices 将流与索引合并
-
Method Details
-
of
Creates a stream from varargs 从可变参数创建Stream- Type Parameters:
T- element type | 元素类型- Parameters:
elements- elements | 元素- Returns:
- stream | 流
-
from
-
from
-
from
-
range
Creates an int range stream 创建整数范围Stream- Parameters:
startInclusive- start (inclusive) | 起始值(包含)endExclusive- end (exclusive) | 结束值(不包含)- Returns:
- int stream | 整数流
-
rangeClosed
Creates an int range stream (closed) 创建闭区间整数范围Stream- Parameters:
startInclusive- start (inclusive) | 起始值(包含)endInclusive- end (inclusive) | 结束值(包含)- Returns:
- int stream | 整数流
-
range
Creates a long range stream 创建长整数范围Stream- Parameters:
startInclusive- start (inclusive) | 起始值(包含)endExclusive- end (exclusive) | 结束值(不包含)- Returns:
- long stream | 长整数流
-
generate
-
iterate
Generates an infinite stream with seed 使用种子生成无限Stream- Type Parameters:
T- element type | 元素类型- Parameters:
seed- initial value | 初始值next- next value function | 下一个值函数- Returns:
- infinite stream | 无限流
-
iterate
Generates a finite stream with seed and predicate 使用种子和谓词生成有限Stream- Type Parameters:
T- element type | 元素类型- Parameters:
seed- initial value | 初始值hasNext- continue predicate | 继续条件next- next value function | 下一个值函数- Returns:
- finite stream | 有限流
-
batch
-
batchStream
Creates a stream of batches 创建批次流- Type Parameters:
T- element type | 元素类型- Parameters:
source- source collection | 源集合batchSize- batch size | 批次大小- Returns:
- stream of batches | 批次流
-
slidingWindow
Creates a sliding window stream 创建滑动窗口流- Type Parameters:
T- element type | 元素类型- Parameters:
source- source collection | 源集合windowSize- window size | 窗口大小- Returns:
- stream of windows | 窗口流
-
slidingWindow
Creates a sliding window stream with custom step 创建自定义步长的滑动窗口流- Type Parameters:
T- element type | 元素类型- Parameters:
source- source collection | 源集合windowSize- window size | 窗口大小step- step size | 步长- Returns:
- stream of windows | 窗口流
-
tumblingWindow
Creates a tumbling window stream (non-overlapping) 创建翻滚窗口流(不重叠)- Type Parameters:
T- element type | 元素类型- Parameters:
source- source collection | 源集合windowSize- window size | 窗口大小- Returns:
- stream of windows | 窗口流
-
zip
Zips two streams into pairs 将两个流合并成对- Type Parameters:
A- first element type | 第一个元素类型B- second element type | 第二个元素类型R- result type | 结果类型- Parameters:
streamA- first stream | 第一个流streamB- second stream | 第二个流zipper- zip function | 合并函数- Returns:
- zipped stream | 合并后的流
-
zipWithIndex
Zips stream with indices 将流与索引合并- Type Parameters:
T- element type | 元素类型- Parameters:
stream- source stream | 源流- Returns:
- stream of indexed elements | 带索引的元素流
-
merge
Merges multiple streams 合并多个流- Type Parameters:
T- element type | 元素类型- Parameters:
streams- streams to merge | 要合并的流- Returns:
- merged stream | 合并后的流
-
interleave
-
filterNulls
-
distinctBy
-
takeWhile
-
dropWhile
-
findFirst
-
anyMatch
-
allMatch
-
noneMatch
-
toUnmodifiableList
-
toUnmodifiableSet
-
toMap
-
toMap
public static <T,K, Map<K,V> V> toMap(Stream<T> stream, Function<T, K> keyMapper, Function<T, V> valueMapper) Collects to map with key and value extractors 使用键和值提取器收集为Map- Type Parameters:
T- element type | 元素类型K- key type | 键类型V- value type | 值类型- Parameters:
stream- source stream | 源流keyMapper- key extractor | 键提取器valueMapper- value extractor | 值提取器- Returns:
- map | Map
-
groupBy
-
partitionBy
-
joining
Joins to string 连接为字符串- Type Parameters:
T- element type | 元素类型- Parameters:
stream- source stream | 源流delimiter- delimiter | 分隔符- Returns:
- joined string | 连接后的字符串
-
joining
public static <T> String joining(Stream<T> stream, CharSequence delimiter, CharSequence prefix, CharSequence suffix) Joins to string with prefix and suffix 使用前缀和后缀连接为字符串- Type Parameters:
T- element type | 元素类型- Parameters:
stream- source stream | 源流delimiter- delimiter | 分隔符prefix- prefix | 前缀suffix- suffix | 后缀- Returns:
- joined string | 连接后的字符串
-
parallelMap
public static <T,R> List<R> parallelMap(Collection<T> source, int parallelism, Function<T, R> mapper) Processes in parallel with limited concurrency 使用有限并发进行并行处理- Type Parameters:
T- input type | 输入类型R- result type | 结果类型- Parameters:
source- source collection | 源集合parallelism- parallelism level | 并行度mapper- transformation function | 转换函数- Returns:
- result list | 结果列表
-
parallelMap
Processes in parallel and collects results 并行处理并收集结果- Type Parameters:
T- input type | 输入类型R- result type | 结果类型- Parameters:
source- source collection | 源集合mapper- transformation function | 转换函数- Returns:
- result list | 结果列表
-
parallelFilter
Filters in parallel 并行过滤- Type Parameters:
T- element type | 元素类型- Parameters:
source- source collection | 源集合predicate- filter condition | 过滤条件- Returns:
- filtered list | 过滤后的列表
-
count
Counts elements 统计元素数量- Type Parameters:
T- element type | 元素类型- Parameters:
stream- source stream | 源流- Returns:
- count | 数量
-
sumInt
Gets sum of integers 获取整数和- Parameters:
stream- source stream | 源流- Returns:
- sum | 和
-
sumLong
Gets sum of longs 获取长整数和- Parameters:
stream- source stream | 源流- Returns:
- sum | 和
-
sumDouble
Gets sum of doubles 获取双精度数和- Parameters:
stream- source stream | 源流- Returns:
- sum | 和
-
averageInt
Gets average of integers 获取整数平均值- Parameters:
stream- source stream | 源流- Returns:
- optional average | 可选平均值
-
max
Gets max element 获取最大元素- Type Parameters:
T- element type | 元素类型- Parameters:
stream- source stream | 源流comparator- comparator | 比较器- Returns:
- optional max | 可选最大值
-
min
Gets min element 获取最小元素- Type Parameters:
T- element type | 元素类型- Parameters:
stream- source stream | 源流comparator- comparator | 比较器- Returns:
- optional min | 可选最小值
-
peek
-
limit
-
skip
-
sorted
Sorts stream 排序流- Type Parameters:
T- element type | 元素类型- Parameters:
stream- source stream | 源流comparator- comparator | 比较器- Returns:
- sorted stream | 排序后的流
-
flatten
-
flattenCollections
Flattens nested collections 展平嵌套集合- Type Parameters:
T- element type | 元素类型- Parameters:
stream- source stream of collections | 集合的源流- Returns:
- flattened stream | 展平后的流
-