aur*_*amo 6 java functional-programming optional java-8 flatmap
我正在尝试使用flatMap OptionalJava.这是一个简化的例子:
List<String> x = Arrays.asList("a", "b", "c");
List<String> result = x.stream().flatMap((val) -> val.equals("b") ? Optional.empty() : Optional.of(val)).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
我从编译器收到此错误消息:
Error:(10, 27) java: incompatible types: no instance(s) of type variable(s) T exist so that java.util.Optional<T> conforms to java.util.stream.Stream<? extends R>
Run Code Online (Sandbox Code Playgroud)
怎么了?以下是我在Scala中尝试实现的示例:
List("a", "b", "c").flatMap(x => if (x == "b") None else Some(x))
Run Code Online (Sandbox Code Playgroud)
它返回:
res2: List[String] = List(a, c)
Run Code Online (Sandbox Code Playgroud)
正如所料.
如何将其转换为Java以便编译?
flatMap期望将输入的元素映射Stream到不同的元素Stream.因此它必须返回a Stream而不是a Optional.
因此,你应该这样做:
List<String> x = Arrays.asList("a", "b", "c");
List<Optional<String>> result =
x.stream()
.flatMap((val) ->
val.equals("b") ? Stream.of(Optional.empty()) :
Stream.of(Optional.of(val)))
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
请注意,如果您的目标只是删除某些值(示例中为"b"),则根本不需要使用Optional.您只需过滤流:
List<String> result =
x.stream()
.filter (val -> !val.equals("b"))
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
这种方式你不需要flatMap,你的输出是一个List<String>而不是一个List<Optional<String>>.
如霍尔格评论的,返回一个该溶液Stream的OptionalS可通过使用简化map的代替flatMap,因为每个元素被映射到单个Optional:
List<String> x = Arrays.asList("a", "b", "c");
List<Optional<String>> result =
x.stream()
.map((val) -> val.equals("b") ? Optional.empty() : Optional.of(val))
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
这里不需要处理Optional。
最简单直接的解决方案是使用filter
List<String> result = x.stream()
.filter(val -> !val.equals("b"))
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
如果您坚持使用flatMap,您应该简单地使用Stream而不是Optional:
List<String> result = x.stream().flatMap(
val -> val.equals("b")? Stream.empty(): Stream.of(val))
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
如果您必须处理不可避免地产生 an 的操作Optional,则必须将其转换为 aStream以便使用Stream.flatMap:
List<String> result = x.stream()
.map(val -> val.equals("b") ? Optional.<String>empty() : Optional.of(val))
.flatMap(o->o.map(Stream::of).orElse(Stream.empty()))
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6312 次 |
| 最近记录: |