Class IterableUtil
java.lang.Object
cloud.opencode.base.collections.IterableUtil
IterableUtil - Iterable Utility Class
IterableUtil - 可迭代对象工具类
Provides comprehensive operations for Iterable including concatenation, partitioning, filtering, transformation, and statistical operations.
提供全面的 Iterable 操作,包括连接、分区、过滤、转换和统计操作。
Features | 主要功能:
- Lazy concatenation of multiple iterables - 惰性连接多个可迭代对象
- Partitioning by size - 按大小分区
- Lazy filtering and transformation - 惰性过滤和转换
- Element access operations - 元素访问操作
- Statistical operations - 统计操作
Usage Examples | 使用示例:
// Concatenate iterables - 连接可迭代对象
Iterable<String> all = IterableUtil.concat(list1, list2, list3);
// Partition into chunks - 分区
Iterable<List<String>> chunks = IterableUtil.partition(list, 10);
// Filter elements - 过滤
Iterable<String> filtered = IterableUtil.filter(list, s -> s.length() > 5);
// Transform elements - 转换
Iterable<Integer> lengths = IterableUtil.transform(list, String::length);
Performance | 性能特性:
- Most operations are lazy - 大多数操作是惰性的
- Memory efficient for large datasets - 对大数据集内存高效
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, Iterable<? extends E> elementsToAdd) Add all elements to a collection 添加到集合static <E> booleanCheck if all elements match the predicate 检查是否全部匹配static <E> booleanCheck if any element matches the predicate 检查是否任意匹配static <E> Iterable<E> Lazily concatenate multiple iterables 懒连接多个 Iterablestatic <E> Iterable<E> Lazily concatenate an iterable of iterables 懒连接 Iterable 的 Iterablestatic booleanCheck if iterable contains an element 检查是否包含static <E> Iterable<E> Cycle through the iterable infinitely 循环迭代static booleanelementsEqual(Iterable<?> iterable1, Iterable<?> iterable2) Check if two iterables have equal elements in order 检查元素相等(顺序敏感)static <E> Iterable<E> Filter elements by type 按类型过滤static <E> Iterable<E> Lazily filter elements 懒过滤static intCount the frequency of an element 统计出现频率static <E> EGet element at position or default 按索引获取元素(带默认值)static <E> EGet element at position 按索引获取元素static <E> EGet the first element or default 获取第一个元素static <E> EGet the last element or default 获取最后一个元素(带默认值)static <E> EGet the last element 获取最后一个元素static <E> EgetOnlyElement(Iterable<? extends E> iterable, E defaultValue) Get the only element from an iterable, or default 查找唯一匹配元素(带默认值)static <E> EgetOnlyElement(Iterable<E> iterable) Get the only element from an iterable 查找唯一匹配元素(多于一个抛异常)static booleanCheck if iterable is empty 检查是否为空static <E> Iterable<E> Limit the number of elements 限制数量paddedPartition(Iterable<E> iterable, int size) Partition iterable into fixed-size chunks, padding the last chunk with null 按固定大小分区(填充最后一组)Partition iterable into fixed-size chunks (view) 按固定大小分区(返回视图)static <E> booleanremoveAll(Iterable<E> removeFrom, Collection<?> elementsToRemove) Remove all matching elements 移除所有匹配元素static <E> booleanRemove elements matching the predicate 移除满足条件的元素static <E> booleanretainAll(Iterable<E> removeFrom, Collection<?> elementsToRetain) Retain all matching elements 保留所有匹配元素static intGet the size of an iterable 计算大小static <E> Iterable<E> Skip the first n elements 跳过前 N 个static <E> E[]Convert to array 转为数组static StringConvert to string representation 转为字符串static <F,T> Iterable <T> Lazily transform elements 懒转换static <E> Optional<E> Try to find the first matching element 查找第一个匹配元素
-
Method Details
-
concat
Lazily concatenate multiple iterables 懒连接多个 Iterable- Type Parameters:
E- element type | 元素类型- Parameters:
inputs- iterables to concatenate | 要连接的可迭代对象- Returns:
- concatenated iterable | 连接后的可迭代对象
-
concat
-
partition
-
paddedPartition
Partition iterable into fixed-size chunks, padding the last chunk with null 按固定大小分区(填充最后一组)- Type Parameters:
E- element type | 元素类型- Parameters:
iterable- the iterable | 可迭代对象size- chunk size | 分区大小- Returns:
- iterable of chunks | 分区的可迭代对象
-
filter
-
filter
Filter elements by type 按类型过滤- Type Parameters:
E- desired element type | 目标元素类型- Parameters:
unfiltered- the unfiltered iterable | 未过滤的可迭代对象desiredType- the desired type | 目标类型- Returns:
- filtered iterable | 过滤后的可迭代对象
-
any
Check if any element matches the predicate 检查是否任意匹配- Type Parameters:
E- element type | 元素类型- Parameters:
iterable- the iterable | 可迭代对象predicate- the predicate | 谓词- Returns:
- true if any element matches | 如果有任何元素匹配则返回 true
-
all
Check if all elements match the predicate 检查是否全部匹配- Type Parameters:
E- element type | 元素类型- Parameters:
iterable- the iterable | 可迭代对象predicate- the predicate | 谓词- Returns:
- true if all elements match | 如果所有元素都匹配则返回 true
-
tryFind
Try to find the first matching element 查找第一个匹配元素- Type Parameters:
E- element type | 元素类型- Parameters:
iterable- the iterable | 可迭代对象predicate- the predicate | 谓词- Returns:
- optional containing the first match | 包含第一个匹配的 Optional
-
getOnlyElement
Get the only element from an iterable 查找唯一匹配元素(多于一个抛异常)- Type Parameters:
E- element type | 元素类型- Parameters:
iterable- the iterable | 可迭代对象- Returns:
- the only element | 唯一元素
- Throws:
NoSuchElementException- if empty | 如果为空OpenCollectionException- if more than one element | 如果超过一个元素
-
getOnlyElement
Get the only element from an iterable, or default 查找唯一匹配元素(带默认值)- Type Parameters:
E- element type | 元素类型- Parameters:
iterable- the iterable | 可迭代对象defaultValue- default value | 默认值- Returns:
- the only element or default | 唯一元素或默认值
- Throws:
OpenCollectionException- if more than one element | 如果超过一个元素
-
getFirst
Get the first element or default 获取第一个元素- Type Parameters:
E- element type | 元素类型- Parameters:
iterable- the iterable | 可迭代对象defaultValue- default value | 默认值- Returns:
- first element or default | 第一个元素或默认值
-
getLast
Get the last element 获取最后一个元素- Type Parameters:
E- element type | 元素类型- Parameters:
iterable- the iterable | 可迭代对象- Returns:
- last element | 最后一个元素
- Throws:
NoSuchElementException- if empty | 如果为空
-
getLast
Get the last element or default 获取最后一个元素(带默认值)- Type Parameters:
E- element type | 元素类型- Parameters:
iterable- the iterable | 可迭代对象defaultValue- default value | 默认值- Returns:
- last element or default | 最后一个元素或默认值
-
get
Get element at position 按索引获取元素- Type Parameters:
E- element type | 元素类型- Parameters:
iterable- the iterable | 可迭代对象position- the position | 位置- Returns:
- element at position | 指定位置的元素
- Throws:
IndexOutOfBoundsException- if position is out of bounds | 如果位置越界
-
get
Get element at position or default 按索引获取元素(带默认值)- Type Parameters:
E- element type | 元素类型- Parameters:
iterable- the iterable | 可迭代对象position- the position | 位置defaultValue- default value | 默认值- Returns:
- element at position or default | 指定位置的元素或默认值
-
transform
public static <F,T> Iterable<T> transform(Iterable<F> fromIterable, Function<? super F, ? extends T> function) Lazily transform elements 懒转换- Type Parameters:
F- from type | 源类型T- to type | 目标类型- Parameters:
fromIterable- the source iterable | 源可迭代对象function- the transform function | 转换函数- Returns:
- transformed iterable | 转换后的可迭代对象
-
limit
-
skip
-
cycle
-
size
Get the size of an iterable 计算大小- Parameters:
iterable- the iterable | 可迭代对象- Returns:
- size | 大小
-
contains
-
frequency
-
isEmpty
Check if iterable is empty 检查是否为空- Parameters:
iterable- the iterable | 可迭代对象- Returns:
- true if empty | 如果为空则返回 true
-
toArray
-
addAll
Add all elements to a collection 添加到集合- Type Parameters:
E- element type | 元素类型- Parameters:
addTo- the target collection | 目标集合elementsToAdd- the elements to add | 要添加的元素- Returns:
- true if collection was modified | 如果集合被修改则返回 true
-
removeAll
Remove all matching elements 移除所有匹配元素- Type Parameters:
E- element type | 元素类型- Parameters:
removeFrom- the source iterable | 源可迭代对象elementsToRemove- the elements to remove | 要移除的元素- Returns:
- true if any elements were removed | 如果有元素被移除则返回 true
-
retainAll
Retain all matching elements 保留所有匹配元素- Type Parameters:
E- element type | 元素类型- Parameters:
removeFrom- the source iterable | 源可迭代对象elementsToRetain- the elements to retain | 要保留的元素- Returns:
- true if any elements were removed | 如果有元素被移除则返回 true
-
removeIf
Remove elements matching the predicate 移除满足条件的元素- Type Parameters:
E- element type | 元素类型- Parameters:
removeFrom- the source iterable | 源可迭代对象predicate- the predicate | 谓词- Returns:
- true if any elements were removed | 如果有元素被移除则返回 true
-
elementsEqual
-
toString
-