【机试题】编写一个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;
    }
}
相关推荐
WiChP18 小时前
【V0.1B5】从零开始的2D游戏引擎开发之路
java·服务器·数据库
cch891818 小时前
汇编与Java:底层与高层的编程对决
java·开发语言·汇编
荒川之神19 小时前
拉链表概念与基本设计
java·开发语言·数据库
cch891819 小时前
汇编与Go:底层到高层的编程差异
java·汇编·golang
chushiyunen19 小时前
python中的@Property和@Setter
java·开发语言·python
禾小西19 小时前
Java中使用正则表达式核心解析
java·python·正则表达式
yoyo_zzm19 小时前
JAVA (Springboot) i18n国际化语言配置
java·spring boot·python
小樱花的樱花19 小时前
C++ new和delete用法详解
linux·开发语言·c++
froginwe1119 小时前
C 运算符
开发语言