Class ListUtil
java.lang.Object
cloud.opencode.base.collections.ListUtil
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 Summary
Modifier and TypeMethodDescriptioncartesianProduct(List<? extends E>... lists) Compute the Cartesian product of lists 计算笛卡尔积cartesianProduct(List<? extends List<? extends E>> lists) Compute the Cartesian product of lists 计算笛卡尔积charactersOf(CharSequence sequence) Return a character list view of a CharSequence 字符列表视图charactersOf(String string) Return a character list view of a string 字符列表视图static <E> ArrayList<E> Create a new ArrayList 创建 ArrayListstatic <E> ArrayList<E> newArrayList(E... elements) Create a new ArrayList with initial elements 创建带初始元素的 ArrayListstatic <E> ArrayList<E> newArrayList(Iterable<? extends E> elements) Create a new ArrayList from an Iterable 从 Iterable 创建 ArrayListstatic <E> ArrayList<E> newArrayListWithCapacity(int initialCapacity) Create a new ArrayList with initial capacity 创建指定容量的 ArrayListstatic <E> ArrayList<E> newArrayListWithExpectedSize(int expectedSize) Create a new ArrayList with expected size 创建预期大小的 ArrayListstatic <E> CopyOnWriteArrayList<E> Create a new CopyOnWriteArrayList 创建 CopyOnWriteArrayListstatic <E> CopyOnWriteArrayList<E> newCopyOnWriteArrayList(Iterable<? extends E> elements) Create a new CopyOnWriteArrayList from an Iterable 从 Iterable 创建 CopyOnWriteArrayListstatic <E> LinkedList<E> Create a new LinkedList 创建 LinkedListstatic <E> LinkedList<E> newLinkedList(Iterable<? extends E> elements) Create a new LinkedList from an Iterable 从 Iterable 创建 LinkedListReturn a partitioned view of the list 分区视图static <E> List<E> Return a reversed view of the list 反转视图static <F,T> List <T> Return a transformed view of the list 转换视图
-
Method Details
-
newArrayList
Create a new ArrayList 创建 ArrayList- Type Parameters:
E- element type | 元素类型- Returns:
- new ArrayList | 新的 ArrayList
-
newArrayList
Create a new ArrayList with initial elements 创建带初始元素的 ArrayList- Type Parameters:
E- element type | 元素类型- Parameters:
elements- initial elements | 初始元素- Returns:
- new ArrayList | 新的 ArrayList
-
newArrayList
-
newArrayListWithCapacity
Create a new ArrayList with initial capacity 创建指定容量的 ArrayList- Type Parameters:
E- element type | 元素类型- Parameters:
initialCapacity- initial capacity | 初始容量- Returns:
- new ArrayList | 新的 ArrayList
-
newArrayListWithExpectedSize
Create a new ArrayList with expected size 创建预期大小的 ArrayList- Type Parameters:
E- element type | 元素类型- Parameters:
expectedSize- expected size | 预期大小- Returns:
- new ArrayList | 新的 ArrayList
-
newLinkedList
Create a new LinkedList 创建 LinkedList- Type Parameters:
E- element type | 元素类型- Returns:
- new LinkedList | 新的 LinkedList
-
newLinkedList
Create a new LinkedList from an Iterable 从 Iterable 创建 LinkedList- Type Parameters:
E- element type | 元素类型- Parameters:
elements- elements | 元素- Returns:
- new LinkedList | 新的 LinkedList
-
newCopyOnWriteArrayList
Create a new CopyOnWriteArrayList 创建 CopyOnWriteArrayList- Type Parameters:
E- element type | 元素类型- Returns:
- new CopyOnWriteArrayList | 新的 CopyOnWriteArrayList
-
newCopyOnWriteArrayList
Create a new CopyOnWriteArrayList from an Iterable 从 Iterable 创建 CopyOnWriteArrayList- Type Parameters:
E- element type | 元素类型- Parameters:
elements- elements | 元素- Returns:
- new CopyOnWriteArrayList | 新的 CopyOnWriteArrayList
-
reverse
-
partition
-
transform
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
-
charactersOf
Return a character list view of a CharSequence 字符列表视图- Parameters:
sequence- the CharSequence | 字符序列- Returns:
- character list | 字符列表
-
cartesianProduct
-
cartesianProduct
Compute the Cartesian product of lists 计算笛卡尔积- Type Parameters:
E- element type | 元素类型- Parameters:
lists- lists to compute product | 要计算积的列表- Returns:
- Cartesian product | 笛卡尔积
-