英文:
Java 8 Stream help when multiple loops
问题
以下是翻译好的内容:
我有一个学生名单,在其中有一个包含三个字段(组、姓名和类别)的课程列表,如下所示:
{
"students": [
{
"classes": [
{
"group": "A",
"name": "studentA",
"category": "COGAT"
},
{
"group": "A",
"name": "studentB",
"category": "COGAT"
},
{
"group": "A",
"name": "studentC",
"category": "OTHER"
}
]
},
{
"classes": [
{
"group": "B",
"name": "studentD",
"category": "COGAT"
},
{
"group": "B",
"name": "studentE",
"category": "COGAT"
},
{
"group": "B",
"name": "studentF",
"category": "COGAT"
}
]
}
]
}
我想在Java 8中使用流(Stream)遍历学生,然后遍历课程,并按组筛选学生。如果匹配到组 A,我想将所有这些学生的姓名添加到新的列表中。有人能帮我实现吗?因为我不确定如何编写嵌套流,然后与某个布尔值进行匹配,然后再添加到列表中。
英文:
I have a list of students and within that, I have a list of classes with the 3 fields-group, name and category as shown below :
{
"students": [
{
"classes": [
{
"group": "A",
"name": "studentA",
"category": "COGAT"
},
{
"group": "A",
"name": "studentB",
"category": "COGAT"
},
{
"field": "A",
"name": "studentC",
"category": "OTHER"
}
]
},
{
"classes": [
{
"group": "B",
"name": "studentD",
"category": "COGAT"
},
{
"group": "B",
"name": "studentE",
"category": "COGAT"
},
{
"group": "B",
"name": "studentF",
"category": "COGAT"
}
]
}
]
}
I want to use Stream in Java 8 and loop through the students and then classes and filter the students by group. If it matches Group A- then I want to add all those students names in the new list. Can someone help me with this? As I am not sure how to write nested streams and then match with some boolean value and then again add to the list.
答案1
得分: 0
List<String> studentNamesInClassA = students.stream()
.filter(student -> student.getClasses().stream().anyMatch(cls -> "A".equals(cls.getGroup())))
.map(student -> student.getName())
.collect(Collectors.toList());
filter
部分筛选出那些满足条件 student.getClasses().stream().anyMatch(cls -> "A".equals(cls.getGroup())))
的学生。然后,map
部分提取出这些学生的姓名,而collect
部分将它们收集到一个列表中。
附:我会将filter
条件提取到一个方法中,例如:
// ...
List<String> studentNamesInClassA = students.stream()
.filter(student -> studentIsInClassA(student))
.map(student -> student.getName())
.collect(Collectors.toList());
// ...
private static boolean studentIsInClassA(Student student) {
return student.getClasses().stream()
.anyMatch(cls -> "A".equals(cls.getGroup()));
}
英文:
List<String> studentNamesInClassA = students.stream()
.filter(student -> student.getClasses().stream().anyMatch(cls -> "A".equals(cls.getGroup())))
.map(student -> student.getName())
.collect(Collectors.toList();
The filter
part filters out only students for which the following student.getClasses().stream().anyMatch(cls -> "A".equals(cls.getGroup()))
results to true
. Then, the map
part "extracts" only the name of those students and the collect
part - well, collects them to a list.
P.S. I'd extract the condition of the filter
in a method. E.g.
// ...
List<String> studentNamesInClassA = students.stream()
.filter(student -> studentIsInClassA(student))
.map(student -> student.getName())
.collect(Collectors.toList();
// ...
private static boolean studentIsInClassA(Student student) {
return student.getClasses().stream()
.anyMatch(cls -> "A".equals(cls.getGroup()));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论