Class Sequence<T>
java.lang.Object
cloud.opencode.base.functional.monad.Sequence<T>
- Type Parameters:
T- element type - 元素类型Security | 安全性:
- Thread-safe: Yes (immutable record) - 线程安全: 是(不可变记录)
- Null-safe: Yes (validates inputs) - 空值安全: 是(验证输入)
- All Implemented Interfaces:
Iterable<T>
Sequence - Lazy evaluated sequence
Sequence - 惰性求值序列
A lazy sequence that computes elements only when needed. Unlike Stream, Sequence can be traversed multiple times. Similar to Kotlin's Sequence or Scala's LazyList.
一个只在需要时计算元素的惰性序列。与 Stream 不同,Sequence 可以多次遍历。 类似于 Kotlin 的 Sequence 或 Scala 的 LazyList。
Features | 主要功能:
- Lazy evaluation - 惰性求值
- Reusable (can traverse multiple times) - 可重用(可多次遍历)
- Memory efficient (no intermediate collections) - 内存高效(无中间集合)
- Short-circuit operations - 短路操作
- Infinite sequences support - 支持无限序列
Usage Examples | 使用示例:
// Create from elements
Sequence<Integer> seq = Sequence.of(1, 2, 3, 4, 5);
// Lazy operations - nothing computed yet
Sequence<Integer> result = seq
.filter(x -> x > 2)
.map(x -> x * 2)
.take(2);
// Computed only when consuming
List<Integer> list = result.toList(); // [6, 8]
// Can be traversed again
int sum = result.fold(0, Integer::sum); // 14
// Infinite sequence
Sequence<Integer> naturals = Sequence.iterate(1, n -> n + 1);
List<Integer> first10 = naturals.take(10).toList();
// Generate sequence
Sequence<Double> randoms = Sequence.generate(Math::random);
Comparison with Stream | 与 Stream 对比:
- Stream: single-use, parallel support - 一次性使用,支持并行
- Sequence: reusable, sequential only - 可重用,仅顺序执行
- Since:
- JDK 25, opencode-base-functional V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordValue with its index. -
Method Summary
Modifier and TypeMethodDescriptionbooleanCheck if all elements match predicate.booleanCheck if any element matches predicate.<R,A> R Collect using a collector.longcount()Count elements.distinct()Remove duplicate elements.drop(int n) Drop the first n elements.Drop elements while predicate is true.static <T> Sequence<T> empty()Create an empty sequence.Filter elements matching the predicate.Filter elements not matching the predicate.Find first element matching predicate.first()Find first element.<R> Sequence<R> Map each element to a sequence and flatten.<R> Rfold(R initial, BiFunction<? super R, ? super T, ? extends R> folder) Fold elements from left with initial value.voidPerform action on each element.static <T> Sequence<T> Create a sequence from iterable.static <T> Sequence<T> fromStream(Stream<T> stream) Create a sequence from stream (single-use).static <T> Sequence<T> Create an infinite sequence from a supplier.static <T> Sequence<T> iterate(T seed, UnaryOperator<T> f) Create an infinite sequence by repeatedly applying a function.iterator()last()Find last element.<R> Sequence<R> Map each element to a new value.booleanCheck if no elements match predicate.static <T> Sequence<T> of(T... elements) Create a sequence from elements.range(int start, int end) Create a sequence of integers from start (inclusive) to end (exclusive).rangeClosed(int start, int end) Create a sequence of integers from start (inclusive) to end (inclusive).reduce(BinaryOperator<T> reducer) Reduce elements (no initial value).sorted()Sort the sequence by natural order.sorted(Comparator<? super T> comparator) Sort the sequence by comparator.take(int n) Take only the first n elements.Take elements while predicate is true.toList()Collect to a list.toSet()Collect to a set.toStream()Convert to stream.<U,R> Sequence <R> zip(Sequence<U> other, BiFunction<? super T, ? super U, ? extends R> zipper) Zip with another sequence.Zip with index.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Iterable
spliterator
-
Method Details
-
empty
Create an empty sequence. 创建空序列。- Type Parameters:
T- element type - 元素类型- Returns:
- empty sequence - 空序列
-
of
Create a sequence from elements. 从元素创建序列。- Type Parameters:
T- element type - 元素类型- Parameters:
elements- the elements - 元素- Returns:
- new sequence - 新序列
-
from
-
fromStream
-
iterate
Create an infinite sequence by repeatedly applying a function. 通过重复应用函数创建无限序列。- Type Parameters:
T- element type - 元素类型- Parameters:
seed- initial value - 初始值f- function to generate next - 生成下一个的函数- Returns:
- infinite sequence - 无限序列
-
generate
-
range
-
rangeClosed
-
map
-
flatMap
-
filter
-
filterNot
-
take
-
takeWhile
-
drop
-
dropWhile
-
distinct
-
sorted
-
sorted
Sort the sequence by comparator. 按比较器排序序列。- Parameters:
comparator- the comparator - 比较器- Returns:
- sorted sequence - 排序后的序列
-
zip
public <U,R> Sequence<R> zip(Sequence<U> other, BiFunction<? super T, ? super U, ? extends R> zipper) Zip with another sequence. 与另一个序列合并。- Type Parameters:
U- other element type - 另一个元素类型R- result type - 结果类型- Parameters:
other- other sequence - 另一个序列zipper- combining function - 组合函数- Returns:
- zipped sequence - 合并后的序列
-
zipWithIndex
Zip with index. 与索引合并。- Returns:
- sequence of indexed elements - 带索引元素的序列
-
fold
Fold elements from left with initial value. 从左侧使用初始值折叠元素。- Type Parameters:
R- result type - 结果类型- Parameters:
initial- initial value - 初始值folder- folding function - 折叠函数- Returns:
- folded result - 折叠结果
-
reduce
Reduce elements (no initial value). 归约元素(无初始值)。- Parameters:
reducer- reducing function - 归约函数- Returns:
- optional result - 可选结果
-
toList
-
toSet
-
collect
-
find
-
first
-
last
-
any
-
all
-
none
-
count
public long count()Count elements. 计数元素。- Returns:
- element count - 元素数量
-
forEach
-
toStream
-
iterator
-