【机试题】编写一个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;
    }
}
相关推荐
Rain的Java大神之路11 分钟前
Transformer 面试与 Java 落地:吃透 Self-Attention,从 QKV 公式到 KV Cache
java·后端·ai编程
sharemylove12 分钟前
Java 常用类 学习笔记
java·开发语言·经验分享·笔记·学习
这个昵称不容易31 分钟前
靓车汽车销售网站设计与实现:SpringBoot 汽车销售 203 个接口,附论文与运行截图
java·springboot·毕设
真上帝的左手1 小时前
7. 网络安全-网关-认证鉴权实战-Spring Boot+SpringSecurity+JWT登录认证
java·spring boot·web安全
多加点辣也没关系10 小时前
JavaScript|第41章:性能优化实战
开发语言·javascript·性能优化
Resean022310 小时前
架构设计系列(十六):DDD 与代码架构
java·开发语言·架构
学长毕设11 小时前
基于SpringBoot 车辆租赁系统平台-附源码
java·spring boot·后端
码途行者11 小时前
每天10分钟学Go:第22课 自定义错误
开发语言·后端·golang
23图11 小时前
C 语言开散列表(拉链法)拆解:链挂得没错,key=-1 会去读 T[-1]
c语言·开发语言·散列表
feixing_fx12 小时前
解构与展开:可读性和性能的取舍
开发语言·前端·javascript·前端框架