【机试题】编写一个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;
    }
}
相关推荐
一个处女座的程序猿O(∩_∩)O4 分钟前
Nacos 中的 Namespace 深度解析:实现多租户隔离的关键机制
java
HeisenbergWDG6 分钟前
线程实现runnable和callable接口
java·开发语言
少控科技17 分钟前
QT新手日记028 QT-QML所有类型
开发语言·qt
JavaGuide23 分钟前
IntelliJ IDEA 2026.1 EAP 发布!拥抱 Java 26,Spring Boot 4 深度支持!
java·后端·mysql·springboot·idea·大厂面试·javaguide
HarmonLTS25 分钟前
Python人工智能深度开发:技术体系、核心实践与工程化落地
开发语言·人工智能·python·算法
丁一郎学编程31 分钟前
测试开发面经
java·开发语言
wjs202433 分钟前
TypeScript 命名空间
开发语言
a程序小傲36 分钟前
京东Java面试被问:RPC调用的熔断降级和自适应限流
java·开发语言·算法·面试·职场和发展·rpc·边缘计算
独自破碎E39 分钟前
MyBatis Flex和MyBatis Plus的区别
java·开发语言·mybatis
葡萄成熟时 !1 小时前
正则表达式
java·正则表达式