【机试题】编写一个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;
    }
}
相关推荐
染指11105 小时前
142.Agent-多Agent框架-通过Skills实现多Agent
开发语言·人工智能·python·langchain·agents
Re_zero5 小时前
一次 SSE 断流排查:PrintWriter 吞掉了我的 IOException
java·后端
好好学习O(∩_∩)O6 小时前
[QT] 多线程moveToThread 和易踩的8个坑
开发语言·qt
我是人✓6 小时前
Java核心语法:作用域、循环与大数值处理
java·开发语言·后端
敲代码的乔帮主6 小时前
Python 3 现代类型标注入门
开发语言·python
夜之眷属6 小时前
PriorityQueue:for-each 遍历和 poll 取出,顺序一样吗?
java·服务器·数据结构·后端·算法
‎ദ്ദിᵔ.˛.ᵔ₎7 小时前
c++11:fuction包装器
开发语言·c++
kimnoic7 小时前
Python 爬虫的原理
开发语言·python
Fluxproxy7 小时前
Python爬虫异步提速终极方案|线程池ThreadPoolExecutor实战,告别单线程慢速爬取
开发语言·爬虫·python
蜗牛互联网7 小时前
Java标准HTTP调用多模态模型的票据抽取前置校验
java·人工智能·python·http