Class FluentList<E>

java.lang.Object
com.sure.tool.fluent.FluentList<E>
Type Parameters:
E - 元素类型

public final class FluentList<E> extends Object
链式集合构建与变换(Fluent API 全面化第二项)。

与 CollUtil 静态方法形态并存:支持「构建 → 变换 → 终端」一条链完成。 中间操作返回新的 FluentList(内部集合不可变,原集合不受影响); 终端 toList() 等返回不可变结果(对标 Guava ImmutableList 理念,零依赖实现)。 元素不允许为 null:任何途径引入 null 元素都会抛 IllegalArgumentException。

示例:

List<String> top = Fluent.of("c", "a", "b", "a")
        .distinct().sorted().limit(2).toList();
// ["a", "b"],不可变
Since:
1.19.0
  • Method Details

    • of

      public static <E> FluentList<E> of()
      创建空列表管道。

      示例:

      FluentList<String> f = Fluent.list();
      
      Type Parameters:
      E - 元素类型
      Returns:
      空管道实例
    • of

      @SafeVarargs public static <E> FluentList<E> of(E... items)
      以指定元素创建列表管道。

      示例:

      FluentList<Integer> f = Fluent.of(3, 1, 2);
      
      Type Parameters:
      E - 元素类型
      Parameters:
      items - 初始元素,不能含 null
      Returns:
      管道实例(内部不可变)
      Throws:
      IllegalArgumentException - 任一元素为 null
    • from

      public static <E> FluentList<E> from(Collection<? extends E> source)
      从既有集合创建列表管道(防御拷贝,原集合修改不影响管道)。

      示例:

      FluentList<String> f = Fluent.of(existingList);
      
      Type Parameters:
      E - 元素类型
      Parameters:
      source - 源集合,不能为 null,不能含 null 元素
      Returns:
      管道实例(内部不可变副本)
      Throws:
      NullPointerException - source 为 null
      IllegalArgumentException - source 含 null 元素
    • add

      public FluentList<E> add(E item)
      追加一个元素。
      Parameters:
      item - 新元素,不能为 null
      Returns:
      新的管道实例
      Throws:
      IllegalArgumentException - item 为 null
    • addAll

      public FluentList<E> addAll(Collection<? extends E> extra)
      追加一批元素。
      Parameters:
      extra - 新元素集合,不能为 null,不能含 null 元素
      Returns:
      新的管道实例
      Throws:
      NullPointerException - extra 为 null
      IllegalArgumentException - extra 含 null 元素
    • map

      public <E2> FluentList<E2> map(Function<? super E, ? extends E2> mapper)
      元素映射(类型可变化)。

      示例:

      List<Integer> lens = Fluent.of("a", "abc").map(String::length).toList();
      // [1, 3]
      
      Type Parameters:
      E2 - 目标元素类型
      Parameters:
      mapper - 映射函数,不能为 null;结果不能为 null
      Returns:
      新的管道实例
      Throws:
      NullPointerException - mapper 为 null
      IllegalArgumentException - 映射结果含 null
    • filter

      public FluentList<E> filter(Predicate<? super E> predicate)
      按条件过滤。
      Parameters:
      predicate - 过滤条件,不能为 null
      Returns:
      新的管道实例
      Throws:
      NullPointerException - predicate 为 null
    • distinct

      public FluentList<E> distinct()
      去重(保持首次出现顺序)。

      示例:

      List<String> d = Fluent.of("a", "b", "a").distinct().toList(); // ["a", "b"]
      
      Returns:
      新的管道实例
    • sorted

      public FluentList<E> sorted()
      自然顺序排序(元素需实现 Comparable,否则 ClassCastException)。
      Returns:
      新的管道实例
    • sorted

      public FluentList<E> sorted(Comparator<? super E> comparator)
      按指定比较器排序。

      示例:

      List<String> s = Fluent.of("b", "a").sorted(Comparator.reverseOrder()).toList();
      // ["b", "a"]
      
      Parameters:
      comparator - 比较器,不能为 null
      Returns:
      新的管道实例
      Throws:
      NullPointerException - comparator 为 null
    • limit

      public FluentList<E> limit(int maxSize)
      限制为前 maxSize 个元素。
      Parameters:
      maxSize - 最大数量,负数抛 IAE;超过原长返回全量
      Returns:
      新的管道实例
      Throws:
      IllegalArgumentException - maxSize < 0
    • skip

      public FluentList<E> skip(int n)
      跳过前 n 个元素。
      Parameters:
      n - 跳过的数量,负数抛 IAE;超过原长返回空
      Returns:
      新的管道实例
      Throws:
      IllegalArgumentException - n < 0
    • reverse

      public FluentList<E> reverse()
      反转顺序。

      示例:

      List<String> r = Fluent.of("a", "b").reverse().toList(); // ["b", "a"]
      
      Returns:
      新的管道实例
    • toList

      public List<E> toList()
      终端:返回不可变列表。

      返回结果不可修改(UnsupportedOperationException), 且为独立副本,与管道内部互不影响。

      Returns:
      不可变 List
    • toArray

      public Object[] toArray()
      终端:返回独立副本数组。

      与 JDK Stream.toArray() 语义一致返回 Object[] (泛型擦除限制)。需要强类型数组时使用 pipe.toList().toArray(new String[0])。

      Returns:
      元素数组副本(Object[])
    • count

      public int count()
      终端:元素个数。
      Returns:
      个数
    • isEmpty

      public boolean isEmpty()
      终端:是否为空。
      Returns:
      空返回 true
    • contains

      public boolean contains(Object o)
      终端:是否包含指定元素(null 返回 false)。
      Parameters:
      o - 目标元素,可为 null(恒返回 false)
      Returns:
      包含返回 true
    • get

      public E get(int index)
      终端:按索引取元素(越界抛 IndexOutOfBoundsException)。
      Parameters:
      index - 索引
      Returns:
      该位置元素
    • join

      public String join(String delimiter)
      终端:以分隔符拼接。

      示例:

      String s = Fluent.of("a", "b", "c").join("-"); // "a-b-c"
      
      Parameters:
      delimiter - 分隔符,不能为 null
      Returns:
      拼接串(空列表返回空串)
      Throws:
      NullPointerException - delimiter 为 null
    • toString

      public String toString()
      Overrides:
      toString in class Object