Java 函数式接口BiConsumer

BiConsumer是一个函数式接口,代表一个接受两个输入参数且不返回任何内容的操作符

java 复制代码
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;

public class BatchOperate<T> {
    private int batchSize=3000;
    private List<T> content=new ArrayList<>();
    private BiConsumer<Integer,List<T>> consumer;
    private int pageNo;
    private long total;

    public void setConsumer(BiConsumer<Integer,List<T>> consumer){
        this.consumer=consumer;
    }

    public void setBatchSize(int batchSize) {
        this.batchSize = batchSize;
    }

    public void addBatch(T t){
        this.content.add(t);
        this.total++;
        if (content.size()>=batchSize){
            this.executeBatch();
        }
    }

    public long getTotal() {
        return total;
    }

    public void executeBatch() {
        if(this.content.size()>0){
            pageNo++;
            consumer.accept(this.pageNo,this.content);
            this.content=new ArrayList<>();
        }
    }

}

使用:

java 复制代码
private void processUser(){
	int size  = 1000;
	BatchOperate<User> batchOperate = new BatchOperate<>();
	batchOperate.setBatchSize(size);
	batchOperate.setConsumer((index, list) -> {
		log.info("######处理用户信息:{}", index);
		removeUserInfo(list);
	});

	for(int i=0; i < 9000; i++){ //生成用户放到 batchOperate
		User user = new User();
		batchOperate.addBatch(user);
	}
	batchOperate.executeBatch();
}


private void removeUserInfo(List<user> userList){
	//处理逻辑
	System.out.println(userList.size());
}
相关推荐
飞花漫卷,情随岁迁28 分钟前
命令进阶与 Playbook 快速入门
java·linux·运维·服务器·网络·数据库·智能路由器
hookserver31 分钟前
企业微信 iPad 协议全功能接入效果实测
java·http·微信·企业微信·ipad
IT枫斗者33 分钟前
缓存和数据库一致性:先更库还是先删缓存,延迟双删怎么落地
java·开发语言·数据库·人工智能·缓存·gateway
播播资源42 分钟前
MiniMax-M4 API 选型指南:接入、思考参数与缓存避坑(含实测数据)
java·spring·缓存
编程见习者44 分钟前
【ReAct企业数据文件分析助手】
人工智能·python
软件技术NINI1 小时前
基于 SpringBoot 的共享植物管理系统
java·spring boot·后端
Vex2une1 小时前
Windows组策略常用配置与安全加固:本地组策略编辑器gpedit.msc实用设置
windows·gpedit.msc·组策略配置·本地组策略编辑器·组策略安全加固·gpupdate生效·windows家庭版组策略
海宇数据1 小时前
零信任架构实战:基于海宇企业司法涉诉构建自动化合同法务审查网关
java·人工智能·架构·自动化
IT研究室1 小时前
最新计算机毕业设计选题推荐-基于spring boot的家政保洁预约系统-网站-文档指导-Java-springboot
java·spring boot·课程设计
FYKJ_20101 小时前
springboot网上汽车租赁系统93523-计算机毕业设计、课程设计
java·vue.js·spring boot·后端·python·汽车·课程设计