Class Streams
java.lang.Object
cloud.opencode.base.collections.Streams
Streams - Enhanced Stream Utilities
Streams - 增强的流工具类
This class provides additional utility methods for working with Java Streams that are not available in the standard library.
该类提供处理 Java 流的额外工具方法,这些方法在标准库中不可用。
Features | 主要功能:
- zip - Combine two streams element-by-element - 逐元素组合两个流
- findLast - Find the last element of a stream - 查找流的最后一个元素
- mapWithIndex - Map with element index - 带索引的映射
- forEachPair - Process consecutive pairs - 处理连续对
- concat - Concatenate multiple streams - 连接多个流
- interleave - Interleave two streams - 交错两个流
Usage Examples | 使用示例:
// Zip two streams | 合并两个流
Stream<String> names = Stream.of("Alice", "Bob");
Stream<Integer> ages = Stream.of(30, 25);
Stream<String> result = Streams.zip(names, ages,
(name, age) -> name + " is " + age);
// ["Alice is 30", "Bob is 25"]
// Find last element | 查找最后一个元素
Optional<String> last = Streams.findLast(Stream.of("a", "b", "c"));
// Optional["c"]
// Map with index | 带索引映射
Stream<String> indexed = Streams.mapWithIndex(
Stream.of("a", "b", "c"),
(element, index) -> index + ": " + element);
// ["0: a", "1: b", "2: c"]
// Process pairs | 处理连续对
Streams.forEachPair(Stream.of(1, 2, 3, 4),
(a, b) -> System.out.println(a + " + " + b + " = " + (a + b)));
// Prints: "1 + 2 = 3", "2 + 3 = 5", "3 + 4 = 7"
Thread Safety | 线程安全:
All methods in this class are stateless and thread-safe.
此类中的所有方法都是无状态和线程安全的。
- 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 recordAn element paired with its index. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Stream<T> Concatenates multiple iterables into one stream.static <T> Stream<T> Concatenates multiple streams into one.static <T> Stream<T> filterWithIndex(Stream<T> stream, BiPredicate<? super T, Long> predicate) Filters elements of a stream based on their index.static <T> Optional<T> Returns the last element of an iterable, if present.static <T> Optional<T> Returns the last element of a stream, if present.static <T> voidforEachPair(Stream<T> stream, BiConsumer<? super T, ? super T> consumer) Processes consecutive pairs of elements in a stream.static <T> Stream<T> interleave(Stream<T> streamA, Stream<T> streamB) Interleaves two streams, alternating between elements from each.static <T,R> Stream <R> mapPairs(Stream<T> stream, BiFunction<? super T, ? super T, ? extends R> mapper) Maps consecutive pairs of elements.static <T,R> Stream <R> mapWithIndex(Stream<T> stream, BiFunction<? super T, Long, ? extends R> mapper) Maps each element of a stream along with its index.static <T> Stream<T> Creates a stream from an iterable.static <T> Stream<T> stream(Enumeration<T> enumeration) Creates a stream from an enumeration.static <T> Stream<T> Creates a stream from an iterator.static <T> Stream<T> Creates a stream from an optional value.static <A,B, R> Stream <R> zip(Collection<A> collectionA, Collection<B> collectionB, BiFunction<? super A, ? super B, ? extends R> combiner) Zips two collections together using the provided combiner function.static <A,B, R> Stream <R> zip(Stream<A> streamA, Stream<B> streamB, BiFunction<? super A, ? super B, ? extends R> combiner) Zips two streams together using the provided combiner function.static <T> Stream<Streams.IndexedElement<T>> zipWithIndex(Stream<T> stream) Zips a stream with its indices, producing pairs of (index, element).
-
Method Details
-
zip
public static <A,B, Stream<R> zipR> (Stream<A> streamA, Stream<B> streamB, BiFunction<? super A, ? super B, ? extends R> combiner) Zips two streams together using the provided combiner function. The resulting stream length is the minimum of the two input stream lengths. 使用提供的组合函数将两个流合并在一起。结果流的长度是两个输入流长度的最小值。- Type Parameters:
A- type of first stream | 第一个流的类型B- type of second stream | 第二个流的类型R- type of result | 结果类型- Parameters:
streamA- first stream | 第一个流streamB- second stream | 第二个流combiner- function to combine elements | 组合元素的函数- Returns:
- zipped stream | 合并后的流
- Throws:
NullPointerException- if any argument is null | 如果任何参数为 null
-
zip
public static <A,B, Stream<R> zipR> (Collection<A> collectionA, Collection<B> collectionB, BiFunction<? super A, ? super B, ? extends R> combiner) Zips two collections together using the provided combiner function. 使用提供的组合函数将两个集合合并在一起。- Type Parameters:
A- type of first collection | 第一个集合的类型B- type of second collection | 第二个集合的类型R- type of result | 结果类型- Parameters:
collectionA- first collection | 第一个集合collectionB- second collection | 第二个集合combiner- function to combine elements | 组合元素的函数- Returns:
- zipped stream | 合并后的流
-
zipWithIndex
Zips a stream with its indices, producing pairs of (index, element). 将流与其索引合并,生成(索引,元素)对。- Type Parameters:
T- element type | 元素类型- Parameters:
stream- the stream | 流- Returns:
- stream of indexed elements | 带索引元素的流
-
findLast
Returns the last element of a stream, if present. 返回流的最后一个元素(如果存在)。This is a terminal operation that consumes the entire stream.
这是一个消耗整个流的终端操作。
- Type Parameters:
T- element type | 元素类型- Parameters:
stream- the stream | 流- Returns:
- the last element, or empty if stream is empty | 最后一个元素,如果流为空则返回空
- Throws:
NullPointerException- if stream is null | 如果流为 null
-
findLast
-
mapWithIndex
public static <T,R> Stream<R> mapWithIndex(Stream<T> stream, BiFunction<? super T, Long, ? extends R> mapper) Maps each element of a stream along with its index. 将流的每个元素与其索引一起映射。- Type Parameters:
T- source element type | 源元素类型R- result element type | 结果元素类型- Parameters:
stream- the stream | 流mapper- function that takes element and index | 接受元素和索引的函数- Returns:
- mapped stream | 映射后的流
-
filterWithIndex
public static <T> Stream<T> filterWithIndex(Stream<T> stream, BiPredicate<? super T, Long> predicate) Filters elements of a stream based on their index. 根据索引过滤流的元素。- Type Parameters:
T- element type | 元素类型- Parameters:
stream- the stream | 流predicate- predicate that takes element and index | 接受元素和索引的谓词- Returns:
- filtered stream | 过滤后的流
-
forEachPair
Processes consecutive pairs of elements in a stream. 处理流中的连续元素对。For a stream [a, b, c, d], this processes (a,b), (b,c), (c,d).
对于流 [a, b, c, d],这会处理 (a,b), (b,c), (c,d)。
- Type Parameters:
T- element type | 元素类型- Parameters:
stream- the stream | 流consumer- consumer for pairs | 对的消费者
-
mapPairs
public static <T,R> Stream<R> mapPairs(Stream<T> stream, BiFunction<? super T, ? super T, ? extends R> mapper) Maps consecutive pairs of elements. 映射连续元素对。- Type Parameters:
T- source element type | 源元素类型R- result element type | 结果元素类型- Parameters:
stream- the stream | 流mapper- function to combine pairs | 组合对的函数- Returns:
- stream of mapped pairs | 映射后的对流
-
concat
Concatenates multiple streams into one. 将多个流连接成一个。- Type Parameters:
T- element type | 元素类型- Parameters:
streams- the streams to concatenate | 要连接的流- Returns:
- concatenated stream | 连接后的流
-
concat
Concatenates multiple iterables into one stream. 将多个可迭代对象连接成一个流。- Type Parameters:
T- element type | 元素类型- Parameters:
iterables- the iterables to concatenate | 要连接的可迭代对象- Returns:
- concatenated stream | 连接后的流
-
interleave
Interleaves two streams, alternating between elements from each. 交错两个流,交替取每个流的元素。For streams [1, 2, 3] and [a, b, c], produces [1, a, 2, b, 3, c].
对于流 [1, 2, 3] 和 [a, b, c],产生 [1, a, 2, b, 3, c]。
- Type Parameters:
T- element type | 元素类型- Parameters:
streamA- first stream | 第一个流streamB- second stream | 第二个流- Returns:
- interleaved stream | 交错后的流
-
stream
-
stream
-
stream
-
stream
Creates a stream from an enumeration. 从枚举创建流。- Type Parameters:
T- element type | 元素类型- Parameters:
enumeration- the enumeration | 枚举- Returns:
- stream of enumeration elements | 枚举元素的流
-