我有一个 dto 类,它存储一些学生 ID 和特定科目的分数。基本上是这样的。
List<StudentInfoDTO> studentInfoDTO = new ArrayList<>();
其中 StudentInfoDTO 如下所示
public class StudentInfoDTO {
Long studentId;
Short marks;
}
现在我想要分数最小的学生 ID。
我尝试了以下但没有给出预期结果。
int smallest = 0;
for(int i = 0; i < studentInfoDTO.size(); i++) {
smallest = studentInfoDTO.get(i).getMarks();
int x = studentInfoDTO.get(i).getMarks();
if (x < smallest) {
smallest = x;
}
}
最佳答案
你也可以使用流,它有一个方便的方法叫做 min()
studentInfoDTO.stream().min(Comparator.comparing(StudentInfoDTO::getMarks));
关于java - java中未排序数组列表中的最小元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52779261/