Class FluentIterable<E>

java.lang.Object
cloud.opencode.base.collections.FluentIterable<E>
Type Parameters:
E - element type | 元素类型
All Implemented Interfaces:
Iterable<E>

public abstract class FluentIterable<E> extends Object implements Iterable<E>
FluentIterable - Fluent API for Iterable Operations FluentIterable - 可迭代操作的流式 API

A fluent wrapper around Iterable that provides chainable operations for filtering, transforming, and collecting elements.

围绕 Iterable 的流式包装器,提供可链式操作用于过滤、转换和收集元素。

Features | 主要功能:

  • Fluent chaining - 流式链接
  • Lazy evaluation - 惰性求值
  • Filter and transform - 过滤和转换
  • Collection conversion - 集合转换
  • Terminal operations - 终端操作

Usage Examples | 使用示例:

// Create from iterable - 从可迭代对象创建
FluentIterable<String> fluent = FluentIterable.from(strings);

// Chain operations - 链式操作
List<Integer> lengths = FluentIterable.from(strings)
    .filter(s -> s.length() > 3)
    .transform(String::length)
    .toList();

// Get first element - 获取第一个元素
Optional<String> first = FluentIterable.from(strings)
    .filter(s -> s.startsWith("A"))
    .first();

// Limit and skip - 限制和跳过
List<String> page = FluentIterable.from(strings)
    .skip(10)
    .limit(5)
    .toList();

Performance | 性能特性:

  • Operations are lazy - 操作是惰性的
  • Single iteration on terminal - 终端操作单次迭代
  • Memory efficient for large collections - 对大集合内存高效

Security | 安全性:

  • Thread-safe: No - 线程安全: 否
  • Null-safe: Yes - 空值安全: 是
Since:
JDK 25, opencode-base-collections V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • FluentIterable

      public FluentIterable()
  • Method Details

    • from

      public static <E> FluentIterable<E> from(Iterable<E> iterable)
      Create a FluentIterable from an Iterable. 从 Iterable 创建 FluentIterable。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      iterable - the iterable | 可迭代对象
      Returns:
      fluent iterable | 流式可迭代对象
    • of

      @SafeVarargs public static <E> FluentIterable<E> of(E... elements)
      Create a FluentIterable from varargs. 从可变参数创建 FluentIterable。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      elements - the elements | 元素
      Returns:
      fluent iterable | 流式可迭代对象
    • empty

      public static <E> FluentIterable<E> empty()
      Create an empty FluentIterable. 创建空 FluentIterable。
      Type Parameters:
      E - element type | 元素类型
      Returns:
      empty fluent iterable | 空流式可迭代对象
    • concat

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

      public static <E> FluentIterable<E> concat(Iterable<? extends Iterable<? extends E>> iterables)
      Concatenate iterables. 连接可迭代对象。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      iterables - iterables to concatenate | 要连接的可迭代对象
      Returns:
      concatenated fluent iterable | 连接的流式可迭代对象
    • filter

      public FluentIterable<E> filter(Predicate<? super E> predicate)
      Filter elements. 过滤元素。
      Parameters:
      predicate - the predicate | 谓词
      Returns:
      filtered fluent iterable | 过滤后的流式可迭代对象
    • filter

      public <T> FluentIterable<T> filter(Class<T> type)
      Filter elements by type. 按类型过滤元素。
      Type Parameters:
      T - target type | 目标类型
      Parameters:
      type - the type | 类型
      Returns:
      filtered fluent iterable | 过滤后的流式可迭代对象
    • transform

      public <T> FluentIterable<T> transform(Function<? super E, T> function)
      Transform elements. 转换元素。
      Type Parameters:
      T - target type | 目标类型
      Parameters:
      function - the transform function | 转换函数
      Returns:
      transformed fluent iterable | 转换后的流式可迭代对象
    • flatMap

      public <T> FluentIterable<T> flatMap(Function<? super E, ? extends Iterable<T>> function)
      Flat map elements. 平面映射元素。
      Type Parameters:
      T - target type | 目标类型
      Parameters:
      function - the function | 函数
      Returns:
      flat mapped fluent iterable | 平面映射后的流式可迭代对象
    • transformAndConcat

      public <T> FluentIterable<T> transformAndConcat(Function<? super E, ? extends Iterable<T>> function)
      Transform and concatenate elements (alias for flatMap). 转换并连接元素(flatMap的别名)。
      Type Parameters:
      T - target type | 目标类型
      Parameters:
      function - the function | 函数
      Returns:
      transformed and concatenated fluent iterable | 转换并连接后的流式可迭代对象
    • limit

      public FluentIterable<E> limit(int maxSize)
      Limit elements. 限制元素数量。
      Parameters:
      maxSize - maximum size | 最大数量
      Returns:
      limited fluent iterable | 限制后的流式可迭代对象
    • skip

      public FluentIterable<E> skip(int count)
      Skip elements. 跳过元素。
      Parameters:
      count - number to skip | 要跳过的数量
      Returns:
      skipped fluent iterable | 跳过后的流式可迭代对象
    • cycle

      public FluentIterable<E> cycle()
      Cycle elements infinitely. 无限循环元素。
      Returns:
      cycling fluent iterable | 循环的流式可迭代对象
    • append

      public FluentIterable<E> append(Iterable<? extends E> other)
      Append elements. 追加元素。
      Parameters:
      other - elements to append | 要追加的元素
      Returns:
      appended fluent iterable | 追加后的流式可迭代对象
    • append

      @SafeVarargs public final FluentIterable<E> append(E... elements)
      Append varargs elements. 追加可变参数元素。
      Parameters:
      elements - elements to append | 要追加的元素
      Returns:
      appended fluent iterable | 追加后的流式可迭代对象
    • distinct

      public FluentIterable<E> distinct()
      Remove duplicate elements. 移除重复元素。
      Returns:
      distinct fluent iterable | 去重后的流式可迭代对象
    • first

      public Optional<E> first()
      Return the first element. 返回第一个元素。
      Returns:
      first element, or empty | 第一个元素,或空
    • firstOr

      public E firstOr(E defaultValue)
      Return the first element, or default. 返回第一个元素,或默认值。
      Parameters:
      defaultValue - default value | 默认值
      Returns:
      first element or default | 第一个元素或默认值
    • last

      public Optional<E> last()
      Return the last element. 返回最后一个元素。
      Returns:
      last element, or empty | 最后一个元素,或空
    • lastOr

      public E lastOr(E defaultValue)
      Return the last element, or default. 返回最后一个元素,或默认值。
      Parameters:
      defaultValue - default value | 默认值
      Returns:
      last element or default | 最后一个元素或默认值
    • get

      public Optional<E> get(int index)
      Get element at index. 获取指定索引的元素。
      Parameters:
      index - the index | 索引
      Returns:
      element at index, or empty | 索引处的元素,或空
    • anyMatch

      public boolean anyMatch(Predicate<? super E> predicate)
      Check if any element matches. 检查是否有元素匹配。
      Parameters:
      predicate - the predicate | 谓词
      Returns:
      true if any match | 如果有匹配则返回 true
    • allMatch

      public boolean allMatch(Predicate<? super E> predicate)
      Check if all elements match. 检查是否所有元素匹配。
      Parameters:
      predicate - the predicate | 谓词
      Returns:
      true if all match | 如果全部匹配则返回 true
    • noneMatch

      public boolean noneMatch(Predicate<? super E> predicate)
      Check if no element matches. 检查是否没有元素匹配。
      Parameters:
      predicate - the predicate | 谓词
      Returns:
      true if none match | 如果没有匹配则返回 true
    • firstMatch

      public Optional<E> firstMatch(Predicate<? super E> predicate)
      Find the first matching element. 找到第一个匹配的元素。
      Parameters:
      predicate - the predicate | 谓词
      Returns:
      first matching element, or empty | 第一个匹配的元素,或空
    • contains

      public boolean contains(Object element)
      Check if contains element. 检查是否包含元素。
      Parameters:
      element - the element | 元素
      Returns:
      true if contains | 如果包含则返回 true
    • size

      public int size()
      Count elements. 计算元素数量。
      Returns:
      count | 数量
    • isEmpty

      public boolean isEmpty()
      Check if empty. 检查是否为空。
      Returns:
      true if empty | 如果为空则返回 true
    • toList

      public List<E> toList()
      Collect to list. 收集为列表。
      Returns:
      list | 列表
    • toImmutableList

      public ImmutableList<E> toImmutableList()
      Collect to immutable list. 收集为不可变列表。
      Returns:
      immutable list | 不可变列表
    • toSet

      public Set<E> toSet()
      Collect to set. 收集为集合。
      Returns:
      set | 集合
    • toImmutableSet

      public ImmutableSet<E> toImmutableSet()
      Collect to immutable set. 收集为不可变集合。
      Returns:
      immutable set | 不可变集合
    • toSortedList

      public ImmutableList<E> toSortedList(Comparator<? super E> comparator)
      Collect to sorted immutable list. 收集为排序的不可变列表。
      Parameters:
      comparator - the comparator | 比较器
      Returns:
      sorted immutable list | 排序的不可变列表
    • toSortedSet

      public ImmutableSortedSet<E> toSortedSet(Comparator<? super E> comparator)
      Collect to sorted immutable set. 收集为排序的不可变集合。
      Parameters:
      comparator - the comparator | 比较器
      Returns:
      sorted immutable set | 排序的不可变集合
    • toMap

      public <K,V> Map<K,V> toMap(Function<? super E, K> keyFunction, Function<? super E, V> valueFunction)
      Collect to map. 收集为映射。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      keyFunction - key function | 键函数
      valueFunction - value function | 值函数
      Returns:
      map | 映射
    • uniqueIndex

      public <K> Map<K,E> uniqueIndex(Function<? super E, K> keyFunction)
      Index elements by unique key. 按唯一键索引元素。
      Type Parameters:
      K - key type | 键类型
      Parameters:
      keyFunction - key function | 键函数
      Returns:
      map with unique keys | 具有唯一键的映射
    • toArray

      public E[] toArray(Class<E> type)
      Convert to array. 转换为数组。
      Parameters:
      type - the array element type | 数组元素类型
      Returns:
      array | 数组
    • copyInto

      public <C extends Collection<? super E>> C copyInto(C collection)
      Copy elements into collection. 将元素复制到集合中。
      Type Parameters:
      C - collection type | 集合类型
      Parameters:
      collection - the target collection | 目标集合
      Returns:
      the collection | 集合
    • collect

      public <R,A> R collect(Collector<? super E, A, R> collector)
      Collect using a collector. 使用收集器收集。
      Type Parameters:
      R - result type | 结果类型
      A - accumulator type | 累加器类型
      Parameters:
      collector - the collector | 收集器
      Returns:
      result | 结果
    • stream

      public Stream<E> stream()
      Convert to stream. 转换为流。
      Returns:
      stream | 流
    • join

      public String join(String separator)
      Join to string. 连接为字符串。
      Parameters:
      separator - separator | 分隔符
      Returns:
      joined string | 连接的字符串