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

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

Java 8 Stream help when multiple loops

问题

以下是翻译好的内容:

我有一个学生名单,在其中有一个包含三个字段(组、姓名和类别)的课程列表,如下所示:

  1. {
  2. "students": [
  3. {
  4. "classes": [
  5. {
  6. "group": "A",
  7. "name": "studentA",
  8. "category": "COGAT"
  9. },
  10. {
  11. "group": "A",
  12. "name": "studentB",
  13. "category": "COGAT"
  14. },
  15. {
  16. "group": "A",
  17. "name": "studentC",
  18. "category": "OTHER"
  19. }
  20. ]
  21. },
  22. {
  23. "classes": [
  24. {
  25. "group": "B",
  26. "name": "studentD",
  27. "category": "COGAT"
  28. },
  29. {
  30. "group": "B",
  31. "name": "studentE",
  32. "category": "COGAT"
  33. },
  34. {
  35. "group": "B",
  36. "name": "studentF",
  37. "category": "COGAT"
  38. }
  39. ]
  40. }
  41. ]
  42. }

我想在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 :

  1. {
  2. "students": [
  3. {
  4. "classes": [
  5. {
  6. "group": "A",
  7. "name": "studentA",
  8. "category": "COGAT"
  9. },
  10. {
  11. "group": "A",
  12. "name": "studentB",
  13. "category": "COGAT"
  14. },
  15. {
  16. "field": "A",
  17. "name": "studentC",
  18. "category": "OTHER"
  19. }
  20. ]
  21. },
  22. {
  23. "classes": [
  24. {
  25. "group": "B",
  26. "name": "studentD",
  27. "category": "COGAT"
  28. },
  29. {
  30. "group": "B",
  31. "name": "studentE",
  32. "category": "COGAT"
  33. },
  34. {
  35. "group": "B",
  36. "name": "studentF",
  37. "category": "COGAT"
  38. }
  39. ]
  40. }
  41. ]
  42. }

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

  1. List<String> studentNamesInClassA = students.stream()
  2. .filter(student -> student.getClasses().stream().anyMatch(cls -> "A".equals(cls.getGroup())))
  3. .map(student -> student.getName())
  4. .collect(Collectors.toList());

filter部分筛选出那些满足条件 student.getClasses().stream().anyMatch(cls -> "A".equals(cls.getGroup()))) 的学生。然后,map部分提取出这些学生的姓名,而collect部分将它们收集到一个列表中。

附:我会将filter条件提取到一个方法中,例如:

  1. // ...
  2. List<String> studentNamesInClassA = students.stream()
  3. .filter(student -> studentIsInClassA(student))
  4. .map(student -> student.getName())
  5. .collect(Collectors.toList());
  6. // ...
  7. private static boolean studentIsInClassA(Student student) {
  8. return student.getClasses().stream()
  9. .anyMatch(cls -> "A".equals(cls.getGroup()));
  10. }
英文:
  1. List&lt;String&gt; studentNamesInClassA = students.stream()
  2. .filter(student -&gt; student.getClasses().stream().anyMatch(cls -&gt; &quot;A&quot;.equals(cls.getGroup())))
  3. .map(student -&gt; student.getName())
  4. .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.

  1. // ...
  2. List&lt;String&gt; studentNamesInClassA = students.stream()
  3. .filter(student -&gt; studentIsInClassA(student))
  4. .map(student -&gt; student.getName())
  5. .collect(Collectors.toList();
  6. // ...
  7. private static boolean studentIsInClassA(Student student) {
  8. return student.getClasses().stream()
  9. .anyMatch(cls -&gt; &quot;A&quot;.equals(cls.getGroup()));
  10. }

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:

确定