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 的这种用法是错误的?
基础教程推荐
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 存储 20 位数字的数据类型 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
