Class PartitionUtil

java.lang.Object
cloud.opencode.base.parallel.batch.PartitionUtil

public final class PartitionUtil extends Object
Partition Util - List Partition Utility 分区工具 - 列表分区工具

Utility class for partitioning lists into smaller chunks for batch processing.

用于将列表分割成较小块以进行批处理的工具类。

Example | 示例:

List<Integer> list = List.of(1, 2, 3, 4, 5, 6, 7);
List<List<Integer>> partitions = PartitionUtil.partition(list, 3);
// [[1, 2, 3], [4, 5, 6], [7]]

Features | 主要功能:

  • List partitioning by size - 按大小分割列表
  • Stream-based lazy partitioning - 基于Stream的延迟分割
  • Collection partitioning - 集合分割

Security | 安全性:

  • Thread-safe: Yes (utility class, stateless) - 线程安全: 是(工具类,无状态)
  • Null-safe: No - 空值安全: 否

Performance | 性能特性:

  • Time complexity: O(n/s) where s is the partition size - creates ceil(n/s) subList views in one loop - 时间复杂度: O(n/s),s 为分区大小 - 单次循环创建 ceil(n/s) 个 subList 视图
  • Space complexity: O(n/s) - result list of partition views; subList is a view with O(1) overhead per partition - 空间复杂度: O(n/s) - 分区视图的结果列表;subList 为视图,每个分区 O(1) 开销
Since:
JDK 25, opencode-base-parallel V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • partition

      public static <T> List<List<T>> partition(List<T> list, int size)
      Partitions a list into smaller lists of specified size. 将列表分割成指定大小的较小列表。
      Type Parameters:
      T - the element type - 元素类型
      Parameters:
      list - the list to partition - 要分割的列表
      size - the partition size - 分区大小
      Returns:
      the list of partitions - 分区列表
      Throws:
      IllegalArgumentException - if size is not positive - 如果大小不是正数
    • partition

      public static <T> List<List<T>> partition(Collection<T> collection, int size)
      Partitions a collection into smaller lists of specified size. 将集合分割成指定大小的较小列表。
      Type Parameters:
      T - the element type - 元素类型
      Parameters:
      collection - the collection to partition - 要分割的集合
      size - the partition size - 分区大小
      Returns:
      the list of partitions - 分区列表
    • partitionStream

      public static <T> Stream<List<T>> partitionStream(List<T> list, int size)
      Creates a stream of partitions (lazy evaluation). 创建分区流(惰性求值)。
      Type Parameters:
      T - the element type - 元素类型
      Parameters:
      list - the list to partition - 要分割的列表
      size - the partition size - 分区大小
      Returns:
      the stream of partitions - 分区流
    • partitionInto

      public static <T> List<List<T>> partitionInto(List<T> list, int count)
      Partitions into a specified number of partitions. 分割成指定数量的分区。
      Type Parameters:
      T - the element type - 元素类型
      Parameters:
      list - the list to partition - 要分割的列表
      count - the number of partitions - 分区数量
      Returns:
      the list of partitions - 分区列表
    • partitionCount

      public static int partitionCount(int totalSize, int partitionSize)
      Calculates the number of partitions. 计算分区数量。
      Parameters:
      totalSize - the total size - 总大小
      partitionSize - the partition size - 分区大小
      Returns:
      the number of partitions - 分区数量
    • getPartition

      public static <T> List<T> getPartition(List<T> list, int partitionSize, int partitionIndex)
      Gets a specific partition by index. 根据索引获取特定分区。
      Type Parameters:
      T - the element type - 元素类型
      Parameters:
      list - the list - 列表
      partitionSize - the partition size - 分区大小
      partitionIndex - the partition index - 分区索引
      Returns:
      the partition - 分区