Class PartitionUtil
java.lang.Object
cloud.opencode.base.collections.transform.PartitionUtil
PartitionUtil - Collection Partitioning Utilities
PartitionUtil - 集合分区工具
Provides utilities for partitioning collections into groups.
提供将集合分区为组的工具。
Features | 主要功能:
- Partition by predicate - 按谓词分区
- Partition by size - 按大小分区
- Partition into n parts - 分区为 n 部分
- Sliding window partitions - 滑动窗口分区
Usage Examples | 使用示例:
List<Integer> numbers = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// Partition by predicate - 按谓词分区
Map<Boolean, List<Integer>> evenOdd = PartitionUtil.partition(numbers, n -> n % 2 == 0);
// Partition by size - 按大小分区
List<List<Integer>> chunks = PartitionUtil.partitionBySize(numbers, 3);
// [[1,2,3], [4,5,6], [7,8,9], [10]]
// Partition into n parts - 分区为 n 部分
List<List<Integer>> parts = PartitionUtil.partitionIntoN(numbers, 3);
Security | 安全性:
- Thread-safe: Yes (stateless utility) - 是(无状态工具)
- Null-safe: No (input must not be null) - 否(输入不能为null)
Performance | 性能特性:
- Time complexity: O(n) for partition and partitionBySize; O(n*w) for slidingWindow where w is the window size - 时间复杂度: partition和partitionBySize为O(n);slidingWindow为O(n*w),w为窗口大小
- Space complexity: O(n) for the resulting partitions - 空间复杂度: O(n),存储分区结果
- Since:
- JDK 25, opencode-base-collections V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> List<T> Drop while predicate is true.Partition into head and tail.Partition into init and last.partition(Collection<T> items, Predicate<? super T> predicate) Partition a collection by a predicate.partitionBy(Collection<T> items, Predicate<? super T>... predicates) Partition a collection by multiple predicates.partitionBySize(Iterable<T> iterable, int size) Partition an iterable into chunks of specified size.partitionBySize(List<T> list, int size) Partition a list into chunks of specified size.partitionIntoN(List<T> list, int n) Partition a list into n approximately equal parts.slidingWindow(List<T> list, int windowSize) Create sliding windows over a list.slidingWindow(List<T> list, int windowSize, int step) Create sliding windows over a list with specified step.Span - partition into takeWhile and dropWhile.Split at specified index.static <T> List<T> Take while predicate is true.
-
Method Details
-
partition
public static <T> Map<Boolean,List<T>> partition(Collection<T> items, Predicate<? super T> predicate) Partition a collection by a predicate. 按谓词分区集合。- Type Parameters:
T- element type | 元素类型- Parameters:
items- the items | 项目predicate- the predicate | 谓词- Returns:
- map with true/false keys | 带有 true/false 键的映射
-
partitionBy
@SafeVarargs public static <T> List<List<T>> partitionBy(Collection<T> items, Predicate<? super T>... predicates) Partition a collection by multiple predicates. 按多个谓词分区集合。- Type Parameters:
T- element type | 元素类型- Parameters:
items- the items | 项目predicates- the predicates | 谓词- Returns:
- list of partitions | 分区列表
-
partitionBySize
-
partitionBySize
-
partitionIntoN
-
slidingWindow
-
slidingWindow
Create sliding windows over a list with specified step. 在列表上创建指定步长的滑动窗口。- Type Parameters:
T- element type | 元素类型- Parameters:
list- the list | 列表windowSize- the window size | 窗口大小step- the step size | 步长- Returns:
- list of windows | 窗口列表
-
headTail
-
initLast
-
splitAt
-
takeWhile
-
dropWhile
-
span
Span - partition into takeWhile and dropWhile. Span - 分区为 takeWhile 和 dropWhile。- Type Parameters:
T- element type | 元素类型- Parameters:
list- the list | 列表predicate- the predicate | 谓词- Returns:
- list of two parts | 两部分的列表
-