【机试题】编写一个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;
    }
}
相关推荐
wddptwd2815 小时前
android studio 报错怎么处理 java.lang.NullPointerException
android·java·android studio
霸道流氓气质16 小时前
分布式系统中接口时序不确定性处理
java·开发语言·分布式
深入云栈16 小时前
Netty 4.2.x 源码深度解析 (四):HashedWheelTimer —— 时间轮定时调度算法
java
带刺的坐椅16 小时前
别再把 Coding Agent 当智能补全了:SolonCode 想做的是数字员工
java·solon·codex·claudecode·opencode·soloncode
库克克16 小时前
【C++】C++11 包装器function 与 绑定器 bind
开发语言·c++
大模型码小白16 小时前
在 Windows 下 Codex 安装、配置与使用详细指南
java·人工智能·windows
多加点辣也没关系16 小时前
JavaScript|第30章:事件机制
开发语言·javascript
小大宇16 小时前
python milvus 案例
开发语言·python·milvus
Gauss松鼠会16 小时前
【GaussDB】GaussDB锁阻塞源头查询
java·开发语言·前端·数据库·算法·gaussdb·经验总结
mingo_敏17 小时前
DeepAgents : 后端(Backends)
java·开发语言