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 的这种用法是错误的?
基础教程推荐
- 控制台应用程序中的 Java 键盘输入解析 2022-01-01
- 如何在相机中应用自定义滤镜 [Surfaceview 预览]. 2022-01-01
- 将 Windows 证书导入 Java 2022-01-01
- 将 double 转换为 Int,向下舍入 2022-01-01
- doFilter()是在servlet的工作完成之前还是之后执行的? 2022-01-01
- JPA惰性列表上的流 2022-01-01
- 在springboot中如何给mybatis加拦截器 2023-04-29
- Java ECDSAwithSHA256 签名长度不一致 2022-01-01
- Maven:无效的目标版本:10 2022-01-01
- 在java中使用xpath和selenium解析HTML表格数据 2022-01-01
