简体   繁体   中英

java generic method signature

I'm a bit confused about the syntax of generic methods. My understanding after reading this post was that generic method should be declared like this:

public static <E> void printArray( E[] inputArray )

with <E> being a placeholder that informs that E is a generic type

So why do I find in the javadoc things like this:

Stream<T> filter(Predicate<? super T> predicate)

No placeholder? I would have expect

<T> Stream<T> filter(Predicate<? super T> predicate)

And why

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

This time there is a placeholder, but only for R and not for T. Why?

These methods are instance methods defined on the interface Stream , which defines the type parameter on the type :

public interface Stream<T> {
  Stream<T> filter(Predicate<? super T>)
}

...so the type parameter is defined on the interface, not the method.

As you noticed, type parameters that aren't defined on the interface are defined on a method-by-method basis, in the case of map .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM