Class PartitionUtil

java.lang.Object
cloud.opencode.base.collections.transform.PartitionUtil

public final class PartitionUtil extends Object
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 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

      public static <T> List<List<T>> partitionBySize(List<T> list, int size)
      Partition a list into chunks of specified size. 将列表分区为指定大小的块。
      Type Parameters:
      T - element type | 元素类型
      Parameters:
      list - the list | 列表
      size - the chunk size | 块大小
      Returns:
      list of chunks | 块列表
    • partitionBySize

      public static <T> List<List<T>> partitionBySize(Iterable<T> iterable, int size)
      Partition an iterable into chunks of specified size. 将可迭代对象分区为指定大小的块。
      Type Parameters:
      T - element type | 元素类型
      Parameters:
      iterable - the iterable | 可迭代对象
      size - the chunk size | 块大小
      Returns:
      list of chunks | 块列表
    • partitionIntoN

      public static <T> List<List<T>> partitionIntoN(List<T> list, int n)
      Partition a list into n approximately equal parts. 将列表分区为 n 个大致相等的部分。
      Type Parameters:
      T - element type | 元素类型
      Parameters:
      list - the list | 列表
      n - the number of parts | 部分数
      Returns:
      list of parts | 部分列表
    • slidingWindow

      public static <T> List<List<T>> slidingWindow(List<T> list, int windowSize)
      Create sliding windows over a list. 在列表上创建滑动窗口。
      Type Parameters:
      T - element type | 元素类型
      Parameters:
      list - the list | 列表
      windowSize - the window size | 窗口大小
      Returns:
      list of windows | 窗口列表
    • slidingWindow

      public static <T> List<List<T>> slidingWindow(List<T> list, int windowSize, int step)
      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

      public static <T> Map<String,Object> headTail(List<T> list)
      Partition into head and tail. 分区为头部和尾部。
      Type Parameters:
      T - element type | 元素类型
      Parameters:
      list - the list | 列表
      Returns:
      map with "head" and "tail" keys | 带有 "head" 和 "tail" 键的映射
    • initLast

      public static <T> Map<String,Object> initLast(List<T> list)
      Partition into init and last. 分区为初始部分和最后元素。
      Type Parameters:
      T - element type | 元素类型
      Parameters:
      list - the list | 列表
      Returns:
      map with "init" and "last" keys | 带有 "init" 和 "last" 键的映射
    • splitAt

      public static <T> List<List<T>> splitAt(List<T> list, int index)
      Split at specified index. 在指定索引处分割。
      Type Parameters:
      T - element type | 元素类型
      Parameters:
      list - the list | 列表
      index - the index | 索引
      Returns:
      list of two parts | 两部分的列表
    • takeWhile

      public static <T> List<T> takeWhile(List<T> list, Predicate<? super T> predicate)
      Take while predicate is true. 在谓词为真时获取。
      Type Parameters:
      T - element type | 元素类型
      Parameters:
      list - the list | 列表
      predicate - the predicate | 谓词
      Returns:
      list of taken elements | 获取的元素列表
    • dropWhile

      public static <T> List<T> dropWhile(List<T> list, Predicate<? super T> predicate)
      Drop while predicate is true. 在谓词为真时丢弃。
      Type Parameters:
      T - element type | 元素类型
      Parameters:
      list - the list | 列表
      predicate - the predicate | 谓词
      Returns:
      remaining elements | 剩余元素
    • span

      public static <T> List<List<T>> span(List<T> list, Predicate<? super T> predicate)
      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 | 两部分的列表