【机试题】编写一个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;
    }
}
相关推荐
java1234_小锋几秒前
String、StringBuilder、StringBuffer的区别?
java·开发语言
星原望野2 分钟前
JAVA集合:List、Set和Map
java·开发语言·list·set·map·集合
2601_9577875818 分钟前
星链引擎矩阵系统:插件化多平台 API 网关与账号级隔离技术实践
java·矩阵·插件化架构
摘星小杨31 分钟前
如何在前端循环调取接口,实时查询数据
开发语言·前端·javascript
yujunl33 分钟前
U9的UI插件客开的总结1
开发语言
多敲代码防脱发1 小时前
Spring进阶(容器实现)
java·开发语言·后端·spring
星辰_mya1 小时前
彩云之上——[特殊字符]的架构师
java·后端·微服务·面试·架构
小新同学^O^1 小时前
简单学习 --> 模型微调
开发语言·人工智能·python·模型微淘
水云桐程序员1 小时前
C++变量的概念及用法
开发语言·c++
phltxy1 小时前
Redis 主从复制
java·数据库·redis