【机试题】编写一个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;
    }
}
相关推荐
只睡四小时2 小时前
LoAF 归因:原生 JS 每次输入 201 次布局读取
开发语言·javascript·性能优化·ecmascript·甘特图·前端性能·loaf
ShineWinsu3 小时前
对于Qt:QMediaPlayer和QVideoWidget的解析
开发语言·c++·qt·视频·客户端·qmediaplayer·媒体播放器
开开心心_Every3 小时前
安卓手写文字生成工具,多种纸张一直免费
服务器·开发语言·jupyter·智能手机·计算机外设·vim·php
打工仔折腾 AI3 小时前
C++ AI SDK(十):DataManager的数据库读写、事务与一致性设计
开发语言·数据库·c++·人工智能·后端·python·性能优化
Forget the Dream3 小时前
Spring AI 2.0 系列(四):Starter 与 OpenAI-compatible:一个不绑死厂商的模型环境
java·人工智能·spring·springai
benchmark_cc3 小时前
vn.py 如何接入 A 股历史行情?使用 QuantDash 映射 BarData 并导入回测数据库
开发语言·数据库·python·pandas·量化交易·股票数据·quantdash
GoodStudyAndDayDayUp3 小时前
Mermaid 流程图基础语法
java·服务器·流程图
dadaobusi3 小时前
学习:noc死锁
java·服务器·前端
旋风菠萝3 小时前
ThreadLocalMap 的 key 使用弱引用?
java