Search code examples
javagenericsreturn-type

return type of generic, when to use <T>returnType<T> and when to just use returnType<T>?


In generic api, why sometimes a type parameter is written before the return type and sometimes it needs not?

Given the following two Steam API as example

Stream<T>   limit(long maxSize)

and

<R> Stream<R>   map(Function<? super T,? extends R> mapper)

Why Stream.limit's return type can just be written as Stream<T> but not <T> Stream<T>?

When to use <T> returnType <T> and when to use returnType<T>?


Solution

  • The first <R> in <R> Stream<R> is just another part of the method signature - the type parameters. You shouldn't view <R> Stream<R> as "one" thing, as you seem to have done in your question title.

    The first <R> is just there to tell you what the type parameters of this method are, so map is a generic method. limit is not a generic method, because it does not have this part in its signature.

    You might be confused about limit isn't a generic method, since it clearly returns something that has a generic parameter T in it. Well, T is actually the generic parameter of the enclosing class Stream<T>, so limit can't really return any other type than the Stream<T> on which it was called. If you call limit on a Stream<A>, you can't expect it to give you a Stream<B>. Therefore, limit is not generic.

    Note that T is also used the parameter list of map.

    when to use <T>returnType<T> and when to just use returnType<T>?

    The big difference here is <T> returnType<T> makes the method generic. returnType<T> is not a generic method, and can only be used if T actually exists in the context.