【机试题】编写一个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;
    }
}
相关推荐
千里码aicood13 小时前
基于Python的电商数据采集系统(大数据+爬虫)
开发语言·python
莫得感情 o13 小时前
并发 14 · 收官:虚拟线程与结构化并发
java·并发
聚美智数13 小时前
图片水印-图片剪裁-图片缩放API接口介绍
java·服务器·数据库
陈皮波比茶13 小时前
Python项目实战-网络机器人(爬虫)
开发语言·网络·爬虫·python·机器人
xierui12312314 小时前
AI Agent 隐私架构:本地化重点为什么是登录态与执行权限
java·人工智能·网络安全·架构
cfm_291414 小时前
Spring AI Tool 调用架构全解
java·人工智能·spring
法哥201214 小时前
用 C++ 控制 CMW100,到底在干什么?
开发语言·c++
sun༒14 小时前
Spring @Scheduled 定时任务详解:Cron表达式、执行顺序、优先级与并行调度
java·后端·spring
QH1392923188014 小时前
NVIDIA H200 H300服务器
运维·服务器·开发语言·网络·信息与通信
一次旅行15 小时前
DeepSeek‑V4‑Flash‑Vision‑Exp 小白入门实战|3种传图方式、完整可跑代码、避坑排障
java·前端·人工智能