Class IteratorUtil
java.lang.Object
cloud.opencode.base.collections.IteratorUtil
IteratorUtil - Iterator Utility Class
IteratorUtil - 迭代器工具类
Provides comprehensive operations for Iterator including creation, concatenation, filtering, transformation, and conversion.
提供全面的迭代器操作,包括创建、连接、过滤、转换和转换。
Features | 主要功能:
- Factory methods for iterators - 迭代器工厂方法
- Concatenation and partitioning - 连接和分区
- Filtering and transformation - 过滤和转换
- PeekingIterator support - PeekingIterator 支持
Usage Examples | 使用示例:
// Create empty iterator - 创建空迭代器
Iterator<String> empty = IteratorUtil.emptyIterator();
// Create singleton iterator - 创建单元素迭代器
Iterator<String> single = IteratorUtil.singletonIterator("hello");
// Create peeking iterator - 创建可查看迭代器
PeekingIterator<String> peeking = IteratorUtil.peekingIterator(list.iterator());
// Filter iterator - 过滤迭代器
Iterator<String> filtered = IteratorUtil.filter(iterator, s -> s.length() > 5);
Performance | 性能特性:
- Most operations are lazy - 大多数操作是惰性的
- No additional memory allocation for transformations - 转换不需要额外内存分配
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 Summary
Modifier and TypeMethodDescriptionstatic <E> booleanaddAll(Collection<E> addTo, Iterator<? extends E> iterator) Add all elements to a collection 添加到集合static intAdvance iterator by n positions 消耗并丢弃 N 个元素static <E> Iterator<E> Concatenate multiple iterators 连接多个迭代器static booleanCheck if iterator contains an element 检查是否包含static <E> Iterator<E> Cycle through the iterable 循环迭代static booleanelementsEqual(Iterator<?> iterator1, Iterator<?> iterator2) Check if two iterators have equal elements 检查元素相等static <E> Iterator<E> Returns an empty iterator 空迭代器static <E> Iterator<E> Filter iterator 过滤static <E> EGet element at position 按索引获取static <E> EGet next element or default 获取下一个元素(带默认值)static <E> Iterator<E> Limit the number of elements 限制数量Partition iterator into chunks 分区static <E> PeekingIterator<E> peekingIterator(Iterator<? extends E> iterator) Create a peeking iterator Peek 迭代器(可查看下一个元素)static <E> booleanremoveAll(Iterator<E> removeFrom, Collection<?> elementsToRemove) Remove all matching elements 移除所有匹配元素static <E> Iterator<E> singletonIterator(E value) Returns a singleton iterator 单元素迭代器static intGet the size (consumes iterator) 计算大小(消耗迭代器)static <E> E[]Convert to array 转为数组static StringConvert to string representation 转为字符串static <F,T> Iterator <T> Transform iterator 转换static <E> Optional<E> Try to find the first matching element 查找static <E> Iterator<E> unmodifiableIterator(Iterator<? extends E> iterator) Returns an unmodifiable iterator 不可变迭代器
-
Method Details
-
emptyIterator
Returns an empty iterator 空迭代器- Type Parameters:
E- element type | 元素类型- Returns:
- empty iterator | 空迭代器
-
singletonIterator
Returns a singleton iterator 单元素迭代器- Type Parameters:
E- element type | 元素类型- Parameters:
value- the single value | 单个值- Returns:
- singleton iterator | 单元素迭代器
-
unmodifiableIterator
-
concat
Concatenate multiple iterators 连接多个迭代器- Type Parameters:
E- element type | 元素类型- Parameters:
inputs- iterators to concatenate | 要连接的迭代器- Returns:
- concatenated iterator | 连接后的迭代器
-
partition
-
filter
-
transform
public static <F,T> Iterator<T> transform(Iterator<F> fromIterator, Function<? super F, ? extends T> function) Transform iterator 转换- Type Parameters:
F- from type | 源类型T- to type | 目标类型- Parameters:
fromIterator- the source iterator | 源迭代器function- the transform function | 转换函数- Returns:
- transformed iterator | 转换后的迭代器
-
tryFind
Try to find the first matching element 查找- Type Parameters:
E- element type | 元素类型- Parameters:
iterator- the iterator | 迭代器predicate- the predicate | 谓词- Returns:
- optional containing the first match | 包含第一个匹配的 Optional
-
get
Get element at position 按索引获取- Type Parameters:
E- element type | 元素类型- Parameters:
iterator- the iterator | 迭代器position- the position | 位置- Returns:
- element at position | 指定位置的元素
- Throws:
IndexOutOfBoundsException- if position is out of bounds | 如果位置越界
-
getNext
Get next element or default 获取下一个元素(带默认值)- Type Parameters:
E- element type | 元素类型- Parameters:
iterator- the iterator | 迭代器defaultValue- default value | 默认值- Returns:
- next element or default | 下一个元素或默认值
-
toArray
-
addAll
Add all elements to a collection 添加到集合- Type Parameters:
E- element type | 元素类型- Parameters:
addTo- the target collection | 目标集合iterator- the iterator | 迭代器- Returns:
- true if collection was modified | 如果集合被修改则返回 true
-
size
Get the size (consumes iterator) 计算大小(消耗迭代器)- Parameters:
iterator- the iterator | 迭代器- Returns:
- size | 大小
-
contains
-
removeAll
Remove all matching elements 移除所有匹配元素- Type Parameters:
E- element type | 元素类型- Parameters:
removeFrom- the source iterator | 源迭代器elementsToRemove- the elements to remove | 要移除的元素- Returns:
- true if any elements were removed | 如果有元素被移除则返回 true
-
elementsEqual
-
toString
-
peekingIterator
Create a peeking iterator Peek 迭代器(可查看下一个元素)- Type Parameters:
E- element type | 元素类型- Parameters:
iterator- the iterator | 迭代器- Returns:
- peeking iterator | 可查看迭代器
-
advance
Advance iterator by n positions 消耗并丢弃 N 个元素- Parameters:
iterator- the iterator | 迭代器numberToAdvance- the number to advance | 前进数量- Returns:
- actual number advanced | 实际前进数量
-
limit
-
cycle
-