【机试题】编写一个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;
    }
}
相关推荐
dsyyyyy11012 分钟前
用JavaScript实现排序算法
开发语言·javascript·排序算法
CHANG_THE_WORLD12 分钟前
逐层拆解:C++ 虚函数从对象内存到手工调用的完整过程
java·开发语言·c++
我登哥MVP13 分钟前
走进 Gang of Four 设计模式:过滤器模式
java·设计模式·过滤器模式
Scott9999HH15 分钟前
2026 避坑实录:国产品牌压力变送器什么牌子好?从硬件抗扰到 C++ Qt 实时曲线绘制源码剖析
开发语言·c++·qt
吃饱了得干活21 分钟前
实时弹幕应该怎么搞?一篇带你从入门到生产级实战
java·后端
geats人山人海30 分钟前
c# 第八章 多态与案例
开发语言·c#
tianyuanwo38 分钟前
深入掌握 java -jar 命令:从基础到 Jenkins Agent 实战
java·jenkins·jar
ch_atu39 分钟前
使用python下载modelscope上的模型
开发语言·python
Full Stack Developme1 小时前
MyBatis-Plus(MP)AST 抽象语法树 设计原理
java·tomcat·mybatis
Albart5751 小时前
Python爬虫请求头伪装后仍被反爬,基于代理池+随机延迟的绕过实战
开发语言·爬虫·python