java - Get element from list that contains another list
问题描述
I have this configuration with java:
Student {
private List<Note> notes;
}
Note {
private int value;
// Constructor - getters - setters
}
School {
Private List<Student> students;
// Constructor - getters - setters
}
I want the following behavior:
Students :
S1 : note1(value=10), note2(value=16)
S2 : note1(value=7), note2(value=18), note3(value=2)
S3 : note1(value=19)
I want to manage an object with a list of schools as:
Manage {
private List<School> schools;
}
And I want to get the school who has the student with the higher note.
In this example: the result would be S3 because we have one student with the higher note 19.
How can I achieve this behavior using Java Stream?
解决方案
您可以创建所有s 和sStream<Map.Entry<School,Student>>
对中的 a ,然后找到具有最大值的条目。School
Student
Student
为此,我建议向Student
类添加一个方法,该方法将返回所有sgetMaxValue()
的最大值。Student
Note
Optional<School> school =
schools.stream()
.flatMap(sc -> sc.getStudents()
.stream()
.map(st -> new SimpleEntry<>(sc,st)))
.collect(Collectors.maxBy(Comparator.comparing(e -> e.getValue().getMaxValue())))
.map(Map.Entry::getKey);
推荐阅读
- powershell - 如何加载组件服务/DCOM 配置管理单元
- python - 获取嵌套模型中的完整字段。Django 休息框架
- typescript - 使用其他界面键的打字稿界面
- docker - pytest:在 setup/teardown 捕获 stdout/stderr
- java - Parcelable 在读取 Serializable 对象时遇到 IOException
- sql - SQL - 朱利安日期 (CYYDDD) 至今
- javascript - *javascript* 将所有 for 循环值提取为不同的变量
- c# - Asp.Net 生产上的调试设置
- python-3.x - 匿名文件,带有指定单词的数组,Python
- wix - 如果在安装程序中修改和升级,如何限制用户更改功能?