【机试题】编写一个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;
    }
}
相关推荐
不懒不懒3 分钟前
【卷积神经网络作业实现人脸的关键点定位功能】
开发语言·python
321.。3 分钟前
Linux 进程控制深度解析:从创建到替换的完整指南
linux·开发语言·c++·学习
酉鬼女又兒6 分钟前
零基础快速入门前端Web存储(sessionStorage & localStorage)知识点详解与蓝桥杯考点应用(可用于备赛蓝桥杯Web应用开发)
开发语言·前端·javascript·职场和发展·蓝桥杯·html
Bert.Cai8 分钟前
Python集合简介
开发语言·python
tryCbest12 分钟前
Java和Python开发项目部署简介
java·开发语言·python
ZTLJQ12 分钟前
任务调度的艺术:Python分布式任务系统完全解析
开发语言·分布式·python
阿里嘎多学长14 分钟前
2026-03-31 GitHub 热点项目精选
开发语言·程序员·github·代码托管
huabiangaozhi19 分钟前
postgresql链接详解
java
小只笨笨狗~22 分钟前
解决objectSpanMethod与expand共存时展开后表格错位问题
开发语言·javascript·ecmascript
比昨天多敲两行27 分钟前
C++ AVL树
开发语言·c++