【机试题】编写一个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;
    }
}
相关推荐
计算机毕设vx_bysj686925 分钟前
计算机毕业设计必看必学~Springboot教学进度管理系统,原创定制程序、单片机、java、PHP、Python、小程序、文案全套、毕设成品等!
java·spring boot·vue·课程设计·管理系统
赵财猫._.28 分钟前
Native API开发:C++与ArkTS混合编程实战
开发语言·c++·harmonyos
普通网友1 小时前
基于C++的操作系统开发
开发语言·c++·算法
狂团商城小师妹1 小时前
JAVA外卖霸王餐CPS优惠CPS平台自主发布小程序+公众号霸王餐源码
java·开发语言·小程序
2501_941111342 小时前
C++中的策略模式高级应用
开发语言·c++·算法
q***11652 小时前
Spring 中的 @ExceptionHandler 注解详解与应用
java·后端·spring
心软小念2 小时前
用Python requests库玩转接口自动化测试!测试工程师的实战秘籍
java·开发语言·python
u***j3243 小时前
后端服务限流实现,Redis+Lua脚本
java·redis·lua
CoderYanger4 小时前
A.每日一题——2536. 子矩阵元素加 1
java·线性代数·算法·leetcode·矩阵
不可描述的两脚兽4 小时前
Redis 快记
java·数据库·redis