问题描述
我正在尝试这样的事情:
I'm trying to do something like this:
Stream<Object> stream = IntStream.of(...)
.flatMapToObj(i -> getStreamOfObjects(i));
不幸的是, IntStream.flatMapToObj()
不即使在Java 9中也存在。
Unfortunately, IntStream.flatMapToObj()
doesn't exist, even in Java 9.
- 为什么遗漏了?
- 什么是推荐的解决方法?
推荐答案
API提供可重用的构建块。这里的相关构建块是 IntStream
, mapToObj
, flatMap
。通过这些,您可以实现您想要的效果:将流映射到对象,然后获得平面地图。提供构建块的排列是不切实际的,并且难以扩展。
The API provides reusable building blocks. The relevant building blocks here are IntStream
, mapToObj
, flatMap
. From these you can achieve what you want: map an in stream to objects, and then get a flat map. Providing permutations of building blocks would not be practical, and harder to extend.
如前所述,使用可用的构建块( mapToObj
+ flatMap
):
As hinted earlier, use the available building blocks (mapToObj
+ flatMap
):
Stream<Object> stream = IntStream.of(...)
.mapToObj(i -> Stream.of(...))
.flatMap(...);
这篇关于为什么没有IntStream.flatMapToObj()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!