Java 8流在多重循环时提供帮助

huangapple go评论66阅读模式
英文:

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&lt;String&gt; studentNamesInClassA = students.stream()
  .filter(student -&gt; student.getClasses().stream().anyMatch(cls -&gt; &quot;A&quot;.equals(cls.getGroup())))
  .map(student -&gt; student.getName())
  .collect(Collectors.toList();

The filter part filters out only students for which the following student.getClasses().stream().anyMatch(cls -&gt; &quot;A&quot;.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&lt;String&gt; studentNamesInClassA = students.stream()
  .filter(student -&gt; studentIsInClassA(student))
  .map(student -&gt; student.getName())
  .collect(Collectors.toList();
// ...

private static boolean studentIsInClassA(Student student) {
    return student.getClasses().stream()
      .anyMatch(cls -&gt; &quot;A&quot;.equals(cls.getGroup()));
}

huangapple
  • 本文由 发表于 2020年10月8日 13:18:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/64256227.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定