All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.hutool.core.collection.PartitionIter Maven / Gradle / Ivy

The newest version!
package cn.hutool.core.collection;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 分批迭代工具,可以分批处理数据
 * 
    *
  1. 比如调用其他客户的接口,传入的入参有限,需要分批
  2. *
  3. 比如mysql/oracle用in语句查询,超过1000可以分批
  4. *
  5. 比如数据库取出游标,可以把游标里的数据一批一批处理
  6. *
* * @param 字段类型 * @author qiqi.chen * @since 5.7.10 */ public class PartitionIter implements IterableIter>, Serializable { private static final long serialVersionUID = 1L; /** * 被分批的迭代器 */ protected final Iterator iterator; /** * 实际每批大小 */ protected final int partitionSize; /** * 创建分组对象 * * @param iterator 迭代器 * @param partitionSize 每批大小,最后一批不满一批算一批 */ public PartitionIter(Iterator iterator, int partitionSize) { this.iterator = iterator; this.partitionSize = partitionSize; } @Override public boolean hasNext() { return this.iterator.hasNext(); } @Override public List next() { final List list = new ArrayList<>(this.partitionSize); for (int i = 0; i < this.partitionSize; i++) { if (false == iterator.hasNext()) { break; } list.add(iterator.next()); } return list; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy