为什么没有IntStream.flatMapToObj()? [英] Why isn't there IntStream.flatMapToObj()?

查看:130
本文介绍了为什么没有IntStream.flatMapToObj()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试这样的事情:

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.


  1. 为什么遗漏了?

  2. 什么是推荐的解决方法?


推荐答案


为什么它被遗漏了?

Why was it was left out?

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.


建议的解决方法是什么?

What's a recommended workaround?

如前所述,使用可用的构建块( mapToObj + flatMap ):

As hinted earlier, use the available building blocks (mapToObj + flatMap):

Stream<Object> stream = IntStream.of(...)
    .mapToObj(i -> Stream.of(...))
    .flatMap(...);

这篇关于为什么没有IntStream.flatMapToObj()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆