Why does the following
<R> Stream<R> map(Function<? super T,? extends R> mapper)
not need to define T as well, as in
<R,T> Stream<R> map(Function<? super T,? extends R> mapper)
Why does the following
<R> Stream<R> map(Function<? super T,? extends R> mapper)
not need to define T as well, as in
<R,T> Stream<R> map(Function<? super T,? extends R> mapper)
interface Stream<T>{ ... <R> Stream<R> map(Function<? super T, ? extends R> modifier); ... }
Because Streams iterate over elements of a given type, T is already known and is visible to all instance level methods (non-static methods in the class). The purpose of the mapping function is to return a Stream which intercepts elements in the base stream, and applies the function before continuing so all elements encountered will now be of type R.
Because the original stream iterates over elements of type T, passing a mapping function which expects inputs of T2 (if it were a method generic instead of class generic) is invalid. The function must expect inputs of type T (or a superclass of T). However, there are no restrictions needed on the output. As such, the function provided depends only on the type of the current stream.
<NEW_STREAM_TYPE> Stream<NEW_STREAM_TYPE> map(Function<? super CURRENT_STREAM_TYPE, ? extends NEW_STREAM_TYPE> modifier);