【机试题】编写一个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;
    }
}
相关推荐
事圆则缓2 分钟前
Kotlin学习导航:从知识树走到项目与面试
android·开发语言·kotlin
程序员夏洛4 分钟前
TCP 是用来解决什么问题?
java·网络协议·http·https
kunicoding5 分钟前
C++ 策略模式实战:写一个可以“一键换输出“的日志系统
开发语言·c++·策略模式
y = xⁿ6 分钟前
拆解cloud code记忆系统
java·人工智能·python·github
纪念 22911 分钟前
C++算法(三)
开发语言·c++·算法
csdn_zhangchunfeng16 分钟前
linux 如何安装rst2man命令
开发语言·chrome·python
轮子大叔20 分钟前
Hibernate6.x详解
java
ClickHouseDB22 分钟前
ClickHouse .NET 驱动程序新特性:从 1.0 到 1.3 的演进之路
java·前端·windows
sejinan25 分钟前
新版!IPTV直播源列表 V1.7 正式发布:接口大厅与会员定制生态解读
开发语言·php
我喜欢就喜欢29 分钟前
DeepSeek Harness 开源贡献手记:从零到合入主线的完整历程
开发语言·人工智能·copilot