为什么 Stream::flatMap 的用法是错误的?

Why is this usage of Stream::flatMap wrong?

我希望能够像这样使用 Stream::flatMap

public static List<String> duplicate(String s) {

    List<String> l = new ArrayList<String>();
    l.add(s);
    l.add(s);

    return l;
}


listOfStrings.stream().flatMap(str -> duplicate(str)).collect(Collectors.toList());

但是我得到以下编译器错误

Test.java:25: error: incompatible types: cannot infer type-variable(s) R listOfStrings.stream().flatMap(str -> duplicate(str)).collect(Collectors.toList());

(argument mismatch; bad return type in lambda expression List cannot be converted to Stream)
where R,T are type-variables: R extends Object declared in method flatMap(Function>) T extends Object declared in interface Stream

在 scala 中我可以做我认为等同的事情

scala> List(1,2,3).flatMap(duplicate(_))
res0: List[Int] = List(1, 1, 2, 2, 3, 3)

为什么这不是 java 中 flatMap 的有效用法?

flatMap中的lambda表达式需要return一个Stream,从flatMap的参数可以看出Function<? super T, ? extends Stream<? extends R>> .

以下代码将编译并且 运行 没问题:

listOfStrings.stream()
             .flatMap(str -> duplicate(str).stream()) // note the .stream() here
             .collect(Collectors.toList());

因为 lambda 表达式 str -> duplicate(str).stream() 的类型是 Function<String, Stream<String>>.

如果您想多次复制流中的每个对象,则不需要为此额外浪费内存 ArrayList。有几个更短更快的替代方案。

  • 使用Stream.generate生成新流,然后限制它:

    listOfStrings.stream()
                 .flatMap(str -> Stream.generate(() -> str).limit(2))
                 .collect(Collectors.toList());
    
  • 通过IntStream.range生成数字序列并将它们映射到相同的字符串:

    listOfStrings.stream()
                 .flatMap(str -> IntStream.range(0, 2).mapToObj(i -> str))
                 .collect(Collectors.toList());
    
  • 使用旧的Collections.nCopies:

    listOfStrings.stream()
                 .flatMap(str -> Collections.nCopies(2, str).stream())
                 .collect(Collectors.toList());
    

如果您确定自己总是会恰好复制两次,那么有一个最短的选择:

listOfStrings.stream()
             .flatMap(str -> Stream.of(str, str))
             .collect(Collectors.toList());