【问题标题】:Using streams Java 8 to modify in List Java在 List Java 中使用流 Java 8 进行修改
【发布时间】:2020-12-04 06:59:07
【问题描述】:

我是 Java 8 的新手。我试图在这里构建学生列表,我想通过从 getStudentNames() 列表中设置学生姓名来返回列表。我想用空格分割这个字符串并循环每个分割的单词,并在下面的 withBadge 中为循环中的每个学生姓名分配每个单词(“orange”、“yellow”、“blue”)。

private static List<Student> buildStudentList(String badge) {
  //TODO: i want to stream the list of Splitted Strings here and modify the list below. 
  return getStudentNames()
                        .stream()
                        .map(name -> new Student.Builder()
                                .withName(name)
                                .withBadge(badge)
                                .build()).collect(Collectors.toList());
        }
        protected static List<StudentNames> getStudentNames() {
            return List.of(“sam”, “andrew”, “joseph”);
        }

我尝试了以下方法:

List<String> splitBadges = Stream.of(badge.split("\\s")).collect(Collectors.toList());

我尝试了上面列表中的每种方法,但它不起作用,因为我已经有一个循环并且想要返回学生列表。它说预期的 void 方法并且无法返回我想要的东西。有人可以在这里帮助如何循环吗?

这是我期望的示例输出:

student: [
    {
      name: "sam",
      badge: "orange"
    },
    {
      name: "andrew",
      badge: "orange"
    },
    {
      name: "joseph",
      badge: "orange"
    },
    {
      name: "sam",
      badge: "yellow"
    },
    {
      name: "andrew",
      badge: "yellow"
    },
    {
      name: "joseph",
      badge: "yellow"
    },
    {
      name: "sam",
      badge: "blue"
    },
    {
      name: "andrew",
      badge: "blue"
    },
    {
      name: "joseph",
      badge: "blue"
    },
   ]

【问题讨论】:

  • 这里有两个问题:1) 当像buildStudentList("orange yellow blue") 这样调用时,您是否希望该方法返回一个包含["sam orange", "andrew yellow", "joseph blue"] 的列表? 2) 您是否尝试保留或修改getStudentNames() 返回的列表?
  • 我在上面做了一些更正。对困惑感到抱歉。我附上了我期望的示例输出......基本上我想循环分割徽章并为每个学生姓名分配每个。
  • 你在找flatMap

标签: java java-8 java-stream


【解决方案1】:

类似这样的东西(导入静态Collection.toList()):

        Stream.of(badges.split("\\s"))
                .map(badge -> studentNames.stream()
                        .map(name -> new Student.Builder()
                                .withName(name)
                                .withBadge(badge)
                                .build())
                        .collect(toList()))
                .flatMap(Collection::stream)
                .collect(toList());

正如 cmets 中所指出的,这可以大大简化并且是一个更好的版本:

Stream.of(badges.split("\\s"))
                .flatMap(badge -> studentNames.stream()
                        .map(name -> new Student.Builder()
                                .withName(name)
                                .withBadge(badge)
                                .build()))
                .collect(toList());

【讨论】:

  • 中间不需要collect(toList()),只需紧接着.flatMap(Collection::stream)。首先只需使用flatMap 而不是map
  • 以上代码对我有用。如果我删除 collect(toList()),它会引发错误“无法从静态上下文引用非静态方法”,不确定它是否相关。但它适用于收集(toList())。有什么想法吗?
  • @shdu12 我相信 Holger 的意思是这样的:Stream.of(badges.split("\\s")).flatMap(badge -&gt; studentNames.stream().map(name -&gt; new Student.Builder().withName(name).withBadge(badge).build())).collect(Collectors.toList()); 更有效。
  • 好的,知道了。谢谢你们的帮助!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-25
  • 1970-01-01
  • 1970-01-01
  • 2019-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多