[英]Why is this usage of Stream::flatMap wrong?
I expected to be able to use Stream::flatMap like this 我希望能够像这样使用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());
But I get the following compiler error 但是我得到以下编译器错误
Test.java:25: error: incompatible types: cannot infer type-variable(s) R listOfStrings.stream().flatMap(str -> duplicate(str)).collect(Collectors.toList());
Test.java:25:错误:不兼容的类型:无法推断类型变量R listOfStrings.stream()。flatMap(str - > duplicate(str))。collect(Collectors.toList());
(argument mismatch; bad return type in lambda expression List cannot be converted to Stream)
(参数不匹配; lambda表达式列表中的错误返回类型无法转换为Stream)
where R,T are type-variables: R extends Object declared in method flatMap(Function>) T extends Object declared in interface Stream其中R,T是类型变量:R extends在方法flatMap中声明的Object(Function>)T extends在接口Stream中声明的Object
In scala I can do what I believe to be equivalent 在scala我可以做我认为相同的事情
scala> List(1,2,3).flatMap(duplicate(_))
res0: List[Int] = List(1, 1, 2, 2, 3, 3)
Why is this not a valid usage of flatMap in java? 为什么这不是java中flatMap的有效用法?
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>>
flatMap
的lambda表达式需要返回一个Stream
, flatMap
的参数可以看出它是Function<? super T, ? extends Stream<? extends R>>
Function<? super T, ? extends Stream<? extends R>>
Function<? super T, ? extends Stream<? extends R>>
. 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());
because the lambda expression str -> duplicate(str).stream()
is of type Function<String, Stream<String>>
. 因为lambda表达式
str -> duplicate(str).stream()
的类型是Function<String, Stream<String>>
。
If you want to duplicate each object in the stream several times, you don't need to waste memory on this with additional ArrayList
. 如果要多次复制流中的每个对象,则不需要使用额外的
ArrayList
在内存上浪费内存。 There are several shorter and faster alternatives. 有几种更短更快的替代品。
Generate new stream using Stream.generate
, then limit it: 使用
Stream.generate
生成新流,然后限制它:
listOfStrings.stream() .flatMap(str -> Stream.generate(() -> str).limit(2)) .collect(Collectors.toList());
Generate sequence of numbers via IntStream.range
and map them to the same string: 通过
IntStream.range
生成数字序列并将它们映射到相同的字符串:
listOfStrings.stream() .flatMap(str -> IntStream.range(0, 2).mapToObj(i -> str)) .collect(Collectors.toList());
Use good old Collections.nCopies
: 使用好的旧
Collections.nCopies
:
listOfStrings.stream() .flatMap(str -> Collections.nCopies(2, str).stream()) .collect(Collectors.toList());
If you are sure that you will always duplicate exactly two times, there's the shortest alternative: 如果你确定你总是会重复两次,那么最短的选择是:
listOfStrings.stream()
.flatMap(str -> Stream.of(str, str))
.collect(Collectors.toList());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.