可以使用Java 8的Stream API来处理这个问题。首先,将数据库查询出来的学生ID和试题ID分别取出,然后使用filter方法来过滤出API中还没有添加到数据库中的数据。

假设数据库查询出来的学生ID和试题ID的集合为dbList,API中获取出的学生ID和试题ID的集合为apiList。代码如下:

import java.util.List;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        // 数据库查询出来的学生ID和试题ID集合
        List<StudentQuestion> dbList = getDBList();

        // API中获取出的学生ID和试题ID集合
        List<StudentQuestion> apiList = getAPIList();

        // 学生ID集合
        List<Integer> dbStudentIds = dbList.stream()
                .map(StudentQuestion::getStudentId)
                .collect(Collectors.toList());

        // 试题ID集合
        List<Integer> dbQuestionIds = dbList.stream()
                .map(StudentQuestion::getQuestionId)
                .collect(Collectors.toList());

        // 过滤出API中还没有添加到数据库中的数据
        List<StudentQuestion> missingData = apiList.stream()
                .filter(studentQuestion ->
                        !dbStudentIds.contains(studentQuestion.getStudentId()) ||
                                !dbQuestionIds.contains(studentQuestion.getQuestionId()))
                .collect(Collectors.toList());

        System.out.println("Missing data: " + missingData);
    }

    private static List<StudentQuestion> getDBList() {
        // 返回数据库查询出来的数据集合
        return null;
    }

    private static List<StudentQuestion> getAPIList() {
        // 返回API中获取出的数据集合
        return null;
    }
}

在上面的代码中,StudentQuestion是一个自定义的类,用于表示学生ID和试题ID的数据。getDBList()getAPIList()方法分别用于获取数据库查询出来的数据集合和API中获取出的数据集合,你需要替换这两个方法的实现来适应你的需求。

最后,通过filter方法过滤出API中还没有添加到数据库中的数据,并将结果收集到一个新的集合中。最后打印出这些缺失的数据

java8将数据库查询出来的list中的学生ID和试题ID分别取出与API中获取出的集合进行学生ID和试题ID对比得到API中还没有添加到数据库中的数据两个LIST都是对象集合

原文地址: https://www.cveoy.top/t/topic/ihfW 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录