Why is this usage of Stream::flatMap wrong?(为什么 Stream::flatMap 的这种用法是错误的?)
问题描述
我希望能够像这样使用 Stream::flatMap
I expected to be able to use Stream::flatMap like this
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());
但我得到以下编译器错误
But I get the following compiler error
Test.java:25:错误:不兼容的类型:无法推断类型变量R 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());
(参数不匹配;lambda 表达式中的返回类型错误列表无法转换为流)
其中 R,T 是类型变量:R 扩展了在方法 flatMap(Function>) 中声明的对象T 扩展接口 Stream 中声明的 Object
(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 中,我可以做我认为等效的事情
In scala I can do what I believe to be equivalent
scala> List(1,2,3).flatMap(duplicate(_))
res0: List[Int] = List(1, 1, 2, 2, 3, 3)
为什么在 java 中这不是 flatMap 的有效用法?
Why is this not a valid usage of flatMap in java?
推荐答案
flatMap
需要返回一个Stream
,可以看出通过 flatMap
类型的参数 Function<?超级T,?扩展流.
The lambda expression in flatMap
needs to return a Stream
, as can be seen by the argument of flatMap
which is of type Function<? super T, ? extends Stream<? extends R>>
.
以下代码将编译并运行良好:
The following code will compile and run fine:
listOfStrings.stream()
.flatMap(str -> duplicate(str).stream()) // note the .stream() here
.collect(Collectors.toList());
因为 lambda 表达式 str ->;duplicate(str).stream()
是 Function
类型.
because the lambda expression str -> duplicate(str).stream()
is of type Function<String, Stream<String>>
.
这篇关于为什么 Stream::flatMap 的这种用法是错误的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么 Stream::flatMap 的这种用法是错误的?


基础教程推荐
- 多个组件的复杂布局 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 从 python 访问 JVM 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 大摇大摆的枚举 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01