From a conceptual point of view, the forEach
method (which, as mentioned in the comments above, specified in the java.lang.Iterable
interface), and the map()
methods are very different.
The main difference is that java.lang.Iterable#forEach(...)
returns nothing, it is void
. Therefore, adding it to the Iterable
interface with the default implementation does not violate anything, and fits well with the logic of this structure.
So far, java.util.stream.Stream#map(...)
returns <R> Stream<R>
.
If I were a Iterable
interface developer and asked to add a map
there, I would first ask: what type should it return? If it is <R> Stream<R>
, so Iterable
not suitable for it. If not, what else?
I think this is the reason.
UPD : @DavidtenHove suggested why not make this map
method return an Iterable<B>
, for example:
default <A, B> Iterable<B> map(Function<A, B> f) {
My opinion: because in this case the Iterable
interface becomes an analog of the Stream
interface, which does not make sense.
forEach
also duplicates the Stream logic, but it fits the Iterable
logic quite well.
source share