Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does <T> T mean when used as a method signature? Is it a return type?

abstract public <T> T iterEdges(EdgeFun<T> func, T accum);

This is for a multithreaded library for graphs. I am not asking for anything pertinent to actual implementation whatsoever, I just don't understand what the double return types mean?

I am just guessing here, but this is my interpretation (I tried googling, but Google doesn't match on non alphanumeric symbols, so I tried a few combinations of terms but didn't get anywhere.)

It is just saying that it will return some kind of collection of T type? Two classes extend the class that this method is found within, and so I guess it allows for polymorphism, but what is the true meaning of this?

like image 916
Arthur Collé Avatar asked Oct 24 '25 17:10

Arthur Collé


2 Answers

It is a generic method. The first is for the generic type that would be used. The function is also declaring that is returns that type T. The parameters are showing that they use that generic parameter type.

like image 91
mkobit Avatar answered Oct 27 '25 08:10

mkobit


T is a generic type. It allows generic type invocation when using the class in actual code.

You may have also seen <K, V> in hash maps. Below is the legend for other parameters

E - Element (used extensively by the Java Collections Framework)
K - Key
N - Number
T - Type
V - Value
S,U,V etc. - 2nd, 3rd, 4th types

http://docs.oracle.com/javase/tutorial/java/generics/types.html

like image 42
eazimmerman Avatar answered Oct 27 '25 07:10

eazimmerman