Why can't Stream.flatMap accept a collection?

随声附和 提交于 2019-11-29 10:17:53

A technical reason, which is not ideal but could be why this wasn't done. You can't overload on a generic type in Java.

They need to support

 Stream.flatMap(Function<Object, Stream<X>> function)

which means they can't overload it with

 Stream.flatMap(Function<Object, Collection<X>> function)

as these two methods have the same signature after erasure.

They could add a method

 Stream.flatMapCollection(Function<Object, Collection<X>> function)

or

 Stream.flatMapIterable(Function<Object, Iterable<X>> function)

 Stream.flatMapI(Function<Object, Iterable<X>> function)

but it wouldn't be pretty.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!