Class IterableUtil

java.lang.Object
cloud.opencode.base.collections.IterableUtil

public final class IterableUtil extends Object
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 Type
    Method
    Description
    static <E> boolean
    addAll(Collection<E> addTo, Iterable<? extends E> elementsToAdd)
    Add all elements to a collection 添加到集合
    static <E> boolean
    all(Iterable<E> iterable, Predicate<? super E> predicate)
    Check if all elements match the predicate 检查是否全部匹配
    static <E> boolean
    any(Iterable<E> iterable, Predicate<? super E> predicate)
    Check if any element matches the predicate 检查是否任意匹配
    static <E> Iterable<E>
    concat(Iterable<? extends E>... inputs)
    Lazily concatenate multiple iterables 懒连接多个 Iterable
    static <E> Iterable<E>
    concat(Iterable<? extends Iterable<? extends E>> inputs)
    Lazily concatenate an iterable of iterables 懒连接 Iterable 的 Iterable
    static boolean
    contains(Iterable<?> iterable, Object element)
    Check if iterable contains an element 检查是否包含
    static <E> Iterable<E>
    cycle(Iterable<E> iterable)
    Cycle through the iterable infinitely 循环迭代
    static boolean
    elementsEqual(Iterable<?> iterable1, Iterable<?> iterable2)
    Check if two iterables have equal elements in order 检查元素相等(顺序敏感)
    static <E> Iterable<E>
    filter(Iterable<?> unfiltered, Class<E> desiredType)
    Filter elements by type 按类型过滤
    static <E> Iterable<E>
    filter(Iterable<E> unfiltered, Predicate<? super E> predicate)
    Lazily filter elements 懒过滤
    static int
    frequency(Iterable<?> iterable, Object element)
    Count the frequency of an element 统计出现频率
    static <E> E
    get(Iterable<? extends E> iterable, int position, E defaultValue)
    Get element at position or default 按索引获取元素(带默认值)
    static <E> E
    get(Iterable<E> iterable, int position)
    Get element at position 按索引获取元素
    static <E> E
    getFirst(Iterable<? extends E> iterable, E defaultValue)
    Get the first element or default 获取第一个元素
    static <E> E
    getLast(Iterable<? extends E> iterable, E defaultValue)
    Get the last element or default 获取最后一个元素(带默认值)
    static <E> E
    getLast(Iterable<E> iterable)
    Get the last element 获取最后一个元素
    static <E> E
    getOnlyElement(Iterable<? extends E> iterable, E defaultValue)
    Get the only element from an iterable, or default 查找唯一匹配元素(带默认值)
    static <E> E
    getOnlyElement(Iterable<E> iterable)
    Get the only element from an iterable 查找唯一匹配元素(多于一个抛异常)
    static boolean
    isEmpty(Iterable<?> iterable)
    Check if iterable is empty 检查是否为空
    static <E> Iterable<E>
    limit(Iterable<E> iterable, int limitSize)
    Limit the number of elements 限制数量
    static <E> Iterable<List<E>>
    paddedPartition(Iterable<E> iterable, int size)
    Partition iterable into fixed-size chunks, padding the last chunk with null 按固定大小分区(填充最后一组)
    static <E> Iterable<List<E>>
    partition(Iterable<E> iterable, int size)
    Partition iterable into fixed-size chunks (view) 按固定大小分区(返回视图)
    static <E> boolean
    removeAll(Iterable<E> removeFrom, Collection<?> elementsToRemove)
    Remove all matching elements 移除所有匹配元素
    static <E> boolean
    removeIf(Iterable<E> removeFrom, Predicate<? super E> predicate)
    Remove elements matching the predicate 移除满足条件的元素
    static <E> boolean
    retainAll(Iterable<E> removeFrom, Collection<?> elementsToRetain)
    Retain all matching elements 保留所有匹配元素
    static int
    size(Iterable<?> iterable)
    Get the size of an iterable 计算大小
    static <E> Iterable<E>
    skip(Iterable<E> iterable, int numberToSkip)
    Skip the first n elements 跳过前 N 个
    static <E> E[]
    toArray(Iterable<? extends E> iterable, Class<E> type)
    Convert to array 转为数组
    static String
    toString(Iterable<?> iterable)
    Convert to string representation 转为字符串
    static <F,T> Iterable<T>
    transform(Iterable<F> fromIterable, Function<? super F, ? extends T> function)
    Lazily transform elements 懒转换
    static <E> Optional<E>
    tryFind(Iterable<E> iterable, Predicate<? super E> predicate)
    Try to find the first matching element 查找第一个匹配元素

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • concat

      @SafeVarargs public static <E> Iterable<E> concat(Iterable<? extends E>... inputs)
      Lazily concatenate multiple iterables 懒连接多个 Iterable
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      inputs - iterables to concatenate | 要连接的可迭代对象
      Returns:
      concatenated iterable | 连接后的可迭代对象
    • concat

      public static <E> Iterable<E> concat(Iterable<? extends Iterable<? extends E>> inputs)
      Lazily concatenate an iterable of iterables 懒连接 Iterable 的 Iterable
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      inputs - iterable of iterables | 可迭代对象的可迭代对象
      Returns:
      concatenated iterable | 连接后的可迭代对象
    • partition

      public static <E> Iterable<List<E>> partition(Iterable<E> iterable, int size)
      Partition iterable into fixed-size chunks (view) 按固定大小分区(返回视图)
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      iterable - the iterable | 可迭代对象
      size - chunk size | 分区大小
      Returns:
      iterable of chunks | 分区的可迭代对象
    • paddedPartition

      public static <E> Iterable<List<E>> paddedPartition(Iterable<E> iterable, int size)
      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

      public static <E> Iterable<E> filter(Iterable<E> unfiltered, Predicate<? super E> predicate)
      Lazily filter elements 懒过滤
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      unfiltered - the unfiltered iterable | 未过滤的可迭代对象
      predicate - the predicate | 谓词
      Returns:
      filtered iterable | 过滤后的可迭代对象
    • filter

      public static <E> Iterable<E> filter(Iterable<?> unfiltered, Class<E> desiredType)
      Filter elements by type 按类型过滤
      Type Parameters:
      E - desired element type | 目标元素类型
      Parameters:
      unfiltered - the unfiltered iterable | 未过滤的可迭代对象
      desiredType - the desired type | 目标类型
      Returns:
      filtered iterable | 过滤后的可迭代对象
    • any

      public static <E> boolean any(Iterable<E> iterable, Predicate<? super E> predicate)
      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

      public static <E> boolean all(Iterable<E> iterable, Predicate<? super E> predicate)
      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

      public static <E> Optional<E> tryFind(Iterable<E> iterable, Predicate<? super E> predicate)
      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

      public static <E> E getOnlyElement(Iterable<E> iterable)
      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

      public static <E> E getOnlyElement(Iterable<? extends E> iterable, E defaultValue)
      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

      public static <E> E getFirst(Iterable<? extends E> iterable, E defaultValue)
      Get the first element or default 获取第一个元素
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      iterable - the iterable | 可迭代对象
      defaultValue - default value | 默认值
      Returns:
      first element or default | 第一个元素或默认值
    • getLast

      public static <E> E getLast(Iterable<E> iterable)
      Get the last element 获取最后一个元素
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      iterable - the iterable | 可迭代对象
      Returns:
      last element | 最后一个元素
      Throws:
      NoSuchElementException - if empty | 如果为空
    • getLast

      public static <E> E getLast(Iterable<? extends E> iterable, E defaultValue)
      Get the last element or default 获取最后一个元素(带默认值)
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      iterable - the iterable | 可迭代对象
      defaultValue - default value | 默认值
      Returns:
      last element or default | 最后一个元素或默认值
    • get

      public static <E> E get(Iterable<E> iterable, int position)
      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

      public static <E> E get(Iterable<? extends E> iterable, int position, E defaultValue)
      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

      public static <E> Iterable<E> limit(Iterable<E> iterable, int limitSize)
      Limit the number of elements 限制数量
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      iterable - the iterable | 可迭代对象
      limitSize - the limit | 限制数量
      Returns:
      limited iterable | 限制后的可迭代对象
    • skip

      public static <E> Iterable<E> skip(Iterable<E> iterable, int numberToSkip)
      Skip the first n elements 跳过前 N 个
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      iterable - the iterable | 可迭代对象
      numberToSkip - the number to skip | 跳过数量
      Returns:
      iterable skipping first n elements | 跳过前 n 个元素的可迭代对象
    • cycle

      public static <E> Iterable<E> cycle(Iterable<E> iterable)
      Cycle through the iterable infinitely 循环迭代
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      iterable - the iterable | 可迭代对象
      Returns:
      cycling iterable | 循环的可迭代对象
    • size

      public static int size(Iterable<?> iterable)
      Get the size of an iterable 计算大小
      Parameters:
      iterable - the iterable | 可迭代对象
      Returns:
      size | 大小
    • contains

      public static boolean contains(Iterable<?> iterable, Object element)
      Check if iterable contains an element 检查是否包含
      Parameters:
      iterable - the iterable | 可迭代对象
      element - the element | 元素
      Returns:
      true if contains | 如果包含则返回 true
    • frequency

      public static int frequency(Iterable<?> iterable, Object element)
      Count the frequency of an element 统计出现频率
      Parameters:
      iterable - the iterable | 可迭代对象
      element - the element | 元素
      Returns:
      frequency | 频率
    • isEmpty

      public static boolean isEmpty(Iterable<?> iterable)
      Check if iterable is empty 检查是否为空
      Parameters:
      iterable - the iterable | 可迭代对象
      Returns:
      true if empty | 如果为空则返回 true
    • toArray

      public static <E> E[] toArray(Iterable<? extends E> iterable, Class<E> type)
      Convert to array 转为数组
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      iterable - the iterable | 可迭代对象
      type - the element type class | 元素类型类
      Returns:
      array | 数组
    • addAll

      public static <E> boolean addAll(Collection<E> addTo, Iterable<? extends E> elementsToAdd)
      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

      public static <E> boolean removeAll(Iterable<E> removeFrom, Collection<?> elementsToRemove)
      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

      public static <E> boolean retainAll(Iterable<E> removeFrom, Collection<?> elementsToRetain)
      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

      public static <E> boolean removeIf(Iterable<E> removeFrom, Predicate<? super E> predicate)
      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

      public static boolean elementsEqual(Iterable<?> iterable1, Iterable<?> iterable2)
      Check if two iterables have equal elements in order 检查元素相等(顺序敏感)
      Parameters:
      iterable1 - first iterable | 第一个可迭代对象
      iterable2 - second iterable | 第二个可迭代对象
      Returns:
      true if equal | 如果相等则返回 true
    • toString

      public static String toString(Iterable<?> iterable)
      Convert to string representation 转为字符串
      Parameters:
      iterable - the iterable | 可迭代对象
      Returns:
      string representation | 字符串表示