cn.hutool.core.collection.PartitionIter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hutool-all Show documentation
Show all versions of hutool-all Show documentation
Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让Java语言也可以“甜甜的”。
package cn.hutool.core.collection;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* 分批迭代工具,可以分批处理数据
*
* - 比如调用其他客户的接口,传入的入参有限,需要分批
* - 比如mysql/oracle用in语句查询,超过1000可以分批
* - 比如数据库取出游标,可以把游标里的数据一批一批处理
*
*
* @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