【机试题】编写一个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;
    }
}
相关推荐
谢亮_vipxieliang26 分钟前
Stream API 不是银弹:常见坑与性能建议
java·开发语言
迅猛龙办公室39 分钟前
实现第一个python程序(HelloWorld)
开发语言·python
源码宝39 分钟前
Web‑PACS 云影像源码|Java+Vue3 医院影像管理系统完整方案
java·源码·影像pacs·成品源码·医院影像系统
phltxy42 分钟前
C 语言动态内存管理:从申请空间到安全释放
c语言·开发语言·ui
可乐鸡翅yeah_1 小时前
M3U8 防盗链 URL‑Token 签名,新手开发实操避坑
开发语言·javascript·ios·音视频·safari
ShineWinsu1 小时前
对于Redis:string类型的解析
java·c++·redis·分布式·缓存·面试·string
2603_965896621 小时前
JS原型与原型链|对象继承与实例底层原理
开发语言·javascript·原型模式
拉格朗日(Lagrange)1 小时前
【第2 章】WorkBuddy 从入门到高手
开发语言·python
yujunl1 小时前
在U9C订单列表上开放多个按钮层叠的问题解决
开发语言
喆星时瑜1 小时前
Win10/11 安装 R 与 RStudio 详细图文教程
开发语言·r语言·安装