【机试题】编写一个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;
    }
}
相关推荐
CCPC不拿奖不改名9 分钟前
数据处理与分析:数据可视化的面试习题
开发语言·python·信息可视化·面试·职场和发展
alonewolf_9910 分钟前
深入解析G1与ZGC垃圾收集器:原理、调优与选型指南
java·jvm·算法
液态不合群12 分钟前
线程池和高并发
开发语言·python
小镇学者12 分钟前
【c++】C++字符串删除末尾字符的三种实现方法
java·开发语言·c++
rfidunion13 分钟前
springboot+VUE+部署(1。新建项目)
java·vue.js·spring boot
小翰子_14 分钟前
Spring Boot整合Sharding-JDBC实现日志表按月按周分表实战
java·spring boot·后端
weixin_3993806921 分钟前
OA 系统假死问题分析与优化
java·运维
SmartRadio24 分钟前
在CH585M代码中如何精细化配置PMU(电源管理单元)和RAM保留
linux·c语言·开发语言·人工智能·单片机·嵌入式硬件·lora
豆沙沙包?41 分钟前
2026年--Lc334-2130. 链表最大孪生和(链表转数组)--java版
java·数据结构·链表
柒.梧.1 小时前
SSM常见核心面试问题深度解析
java·spring·面试·职场和发展·mybatis