【机试题】编写一个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;
    }
}
相关推荐
至为芯16 小时前
IP6537至为芯支持双C口快充输出的45W降压SOC芯片
c语言·开发语言
kk哥889916 小时前
如何快速掌握JavaSE的核心语法?
java
我是一只小青蛙88817 小时前
AVL树:平衡二叉搜索树原理与C++实战
java·jvm·面试
小羊羊Python17 小时前
SoundMaze v1.0.1正式发布!
开发语言·c++
浩瀚地学17 小时前
【Java】JDK8的一些新特性
java·开发语言·经验分享·笔记·学习
l1t17 小时前
利用DeepSeek将python DLX求解数独程序格式化并改成3.x版本
开发语言·python·算法·数独
XXOOXRT18 小时前
基于SpringBoot的加法计算器
java·spring boot·后端·html5
阿崽meitoufa18 小时前
JVM虚拟机:垃圾收集器和判断对象是否存活的算法
java·jvm·算法
yugi98783818 小时前
基于遗传算法优化主动悬架模糊控制的Matlab实现
开发语言·matlab