【机试题】编写一个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;
    }
}
相关推荐
white-persist5 分钟前
Python实例方法与Python类的构造方法全解析
开发语言·前端·python·原型模式
千里马-horse7 分钟前
Async++ 源码分析8--partitioner.h
开发语言·c++·async++·partitioner
Java 码农30 分钟前
Centos7 maven 安装
java·python·centos·maven
harmful_sheep39 分钟前
maven mvn 安装自定义 jar 包
java·maven·jar
Lucis__1 小时前
再探类&对象——C++入门进阶
开发语言·c++
007php0071 小时前
某大厂跳动面试:计算机网络相关问题解析与总结
java·开发语言·学习·计算机网络·mysql·面试·职场和发展
JH30731 小时前
第七篇:Buffer Pool 与 InnoDB 其他组件的协作
java·数据库·mysql·oracle
lsx2024062 小时前
HTML 字符集
开发语言
很㗊2 小时前
C与C++---类型转换
c语言·开发语言
say_fall2 小时前
精通C语言(3. 自定义类型:联合体和枚举)
c语言·开发语言