Class ListUtil

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

public final class ListUtil extends Object
ListUtil - List Utility Class ListUtil - 列表工具类

Provides factory methods and operations for List implementations including ArrayList, LinkedList, and CopyOnWriteArrayList.

提供 List 实现的工厂方法和操作,包括 ArrayList、LinkedList 和 CopyOnWriteArrayList。

Features | 主要功能:

  • Factory methods for list creation - 列表创建工厂方法
  • View operations (reverse, partition, transform) - 视图操作(反转、分区、转换)
  • Cartesian product - 笛卡尔积
  • Character list from strings - 字符列表

Usage Examples | 使用示例:

// Create ArrayList - 创建 ArrayList
List<String> list = ListUtil.newArrayList("a", "b", "c");

// Reverse view - 反转视图
List<String> reversed = ListUtil.reverse(list);

// Partition - 分区
List<List<String>> partitions = ListUtil.partition(list, 2);

// Transform view - 转换视图
List<Integer> lengths = ListUtil.transform(list, String::length);

// Cartesian product - 笛卡尔积
List<List<String>> product = ListUtil.cartesianProduct(list1, list2);

Performance | 性能特性:

  • Factory methods: O(n) - 工厂方法: O(n)
  • View operations: O(1) creation, O(n) iteration - 视图操作: O(1) 创建, O(n) 遍历
  • Cartesian product: O(n^k) for k lists - 笛卡尔积: O(n^k) 对于 k 个列表

Security | 安全性:

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

    • newArrayList

      public static <E> ArrayList<E> newArrayList()
      Create a new ArrayList 创建 ArrayList
      Type Parameters:
      E - element type | 元素类型
      Returns:
      new ArrayList | 新的 ArrayList
    • newArrayList

      @SafeVarargs public static <E> ArrayList<E> newArrayList(E... elements)
      Create a new ArrayList with initial elements 创建带初始元素的 ArrayList
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      elements - initial elements | 初始元素
      Returns:
      new ArrayList | 新的 ArrayList
    • newArrayList

      public static <E> ArrayList<E> newArrayList(Iterable<? extends E> elements)
      Create a new ArrayList from an Iterable 从 Iterable 创建 ArrayList
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      elements - elements | 元素
      Returns:
      new ArrayList | 新的 ArrayList
    • newArrayListWithCapacity

      public static <E> ArrayList<E> newArrayListWithCapacity(int initialCapacity)
      Create a new ArrayList with initial capacity 创建指定容量的 ArrayList
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      initialCapacity - initial capacity | 初始容量
      Returns:
      new ArrayList | 新的 ArrayList
    • newArrayListWithExpectedSize

      public static <E> ArrayList<E> newArrayListWithExpectedSize(int expectedSize)
      Create a new ArrayList with expected size 创建预期大小的 ArrayList
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      expectedSize - expected size | 预期大小
      Returns:
      new ArrayList | 新的 ArrayList
    • newLinkedList

      public static <E> LinkedList<E> newLinkedList()
      Create a new LinkedList 创建 LinkedList
      Type Parameters:
      E - element type | 元素类型
      Returns:
      new LinkedList | 新的 LinkedList
    • newLinkedList

      public static <E> LinkedList<E> newLinkedList(Iterable<? extends E> elements)
      Create a new LinkedList from an Iterable 从 Iterable 创建 LinkedList
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      elements - elements | 元素
      Returns:
      new LinkedList | 新的 LinkedList
    • newCopyOnWriteArrayList

      public static <E> CopyOnWriteArrayList<E> newCopyOnWriteArrayList()
      Create a new CopyOnWriteArrayList 创建 CopyOnWriteArrayList
      Type Parameters:
      E - element type | 元素类型
      Returns:
      new CopyOnWriteArrayList | 新的 CopyOnWriteArrayList
    • newCopyOnWriteArrayList

      public static <E> CopyOnWriteArrayList<E> newCopyOnWriteArrayList(Iterable<? extends E> elements)
      Create a new CopyOnWriteArrayList from an Iterable 从 Iterable 创建 CopyOnWriteArrayList
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      elements - elements | 元素
      Returns:
      new CopyOnWriteArrayList | 新的 CopyOnWriteArrayList
    • reverse

      public static <E> List<E> reverse(List<E> list)
      Return a reversed view of the list 反转视图
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      list - the list | 列表
      Returns:
      reversed view | 反转视图
    • partition

      public static <E> List<List<E>> partition(List<E> list, int size)
      Return a partitioned view of the list 分区视图
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      list - the list | 列表
      size - partition size | 分区大小
      Returns:
      list of partitions | 分区列表
    • transform

      public static <F,T> List<T> transform(List<F> fromList, Function<? super F, ? extends T> function)
      Return a transformed view of the list 转换视图
      Type Parameters:
      F - from type | 源类型
      T - to type | 目标类型
      Parameters:
      fromList - the source list | 源列表
      function - the transform function | 转换函数
      Returns:
      transformed view | 转换视图
    • charactersOf

      public static List<Character> charactersOf(String string)
      Return a character list view of a string 字符列表视图
      Parameters:
      string - the string | 字符串
      Returns:
      character list | 字符列表
    • charactersOf

      public static List<Character> charactersOf(CharSequence sequence)
      Return a character list view of a CharSequence 字符列表视图
      Parameters:
      sequence - the CharSequence | 字符序列
      Returns:
      character list | 字符列表
    • cartesianProduct

      public static <E> List<List<E>> cartesianProduct(List<? extends List<? extends E>> lists)
      Compute the Cartesian product of lists 计算笛卡尔积
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      lists - lists to compute product | 要计算积的列表
      Returns:
      Cartesian product | 笛卡尔积
    • cartesianProduct

      @SafeVarargs public static <E> List<List<E>> cartesianProduct(List<? extends E>... lists)
      Compute the Cartesian product of lists 计算笛卡尔积
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      lists - lists to compute product | 要计算积的列表
      Returns:
      Cartesian product | 笛卡尔积