【机试题】编写一个Java函数,实现批量获取数据的功能

题目:编写一个Java函数,实现批量获取数据的功能(BService.get(List ids))。具体要求如下:

1)提供一个函数BService.get(List ids),支持最多传入100个id;

2)在BService.get((List ids)函数内部,需要将传入的id列表分批(每批10个id)进行调用AService.get(List ids)函数获取数据;

3)BService.get((List ids)函数需要返回所有批次获取的数据的合并结果,即一个包含所有数据的List;

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

public class BService {
    private AService aService;

    public BService(AService aService) {
        this.aService = aService;
    }

    public List<Integer> get(List<Integer> ids) {
        List<Integer> result = new ArrayList<>();
        if (ids == null || ids.size() == 0) {
            return result;
        }
        int batchSize = 10;
        int batchCount = (ids.size() + batchSize - 1) / batchSize;
        for (int i = 0; i < batchCount; i++) {
            int start = i * batchSize;
            int end = Math.min(start + batchSize, ids.size());
            List<Integer> batchIds = ids.subList(start, end);
            List<Integer> batchResult = aService.get(batchIds);
            result.addAll(batchResult);
        }
        return result;
    }
}
相关推荐
Listen·Rain18 分钟前
Springboot整合Ollama实现调用本地多模型
java·spring boot·后端
霸道流氓气质18 分钟前
Java 工程师 AI 智能体(Agent)完整学习路线
java·人工智能·学习
天天进步201531 分钟前
Python全栈项目--校园食堂点餐与推荐系统
开发语言·python
aaPIXa62232 分钟前
C++模板元编程:编译期计算Fibonacci数列
java·开发语言·c++
SunnyDays101139 分钟前
Java 编辑 PDF:从增删改查到加密保护
java·修改 pdf·编辑 pdf
eybk1 小时前
写一个可以编制pdf文件的python程序
开发语言·python·pdf
球king1 小时前
CC GUI 插件:在 IDEA 中使用 CodeX
java·ide·intellij-idea
cui_ruicheng1 小时前
Python从入门到实战(八):封装、多态与抽象类
开发语言·python
apihz1 小时前
台风实时与历史详情查询免费 API 接口完整教程
android·开发语言·tcp/ip·dubbo·台风·天气预报
知无不研1 小时前
c语言和c++中的静态关键字
开发语言·c++·静态关键字