public static <T extends CharSequence> Collection<T> getLongWords(Collection<T extends CharSequence> coll)
You have already specified the bound on T at the beginning of the method declaration, you cannot refine it (or respecify it) in the method's argument. This is OK:
public static <T extends CharSequence> Collection<T> getLongWords(Collection<? extends CharSequence> coll)
Well, that gets the compilation of that line fixed anyway. I have not checked whether it makes sense to do so in the context of the full question. [ April 15, 2007: Message edited by: Barry Gaunt ]
I think Sigrid is unclearof the difference between typesafeing a collection and templating a method.
when you are declaring a type safe collection you can use notation like: ArrayList<? extends CharSequence> ,
but when you are templating a method or a class you use notation like <T extends CharSequence>
also be careful of the the method below, the collection passed to the method will not determine what T is. It is left to the return type to do this. ie you could pass in Collection<StringBuffer> and return Collection<String>.
public static <T extends CharSequence> Collection<T> getLongWords(Collection<? extends CharSequence> coll)
in the method below the type of collection passed and returned would be the same:
public static <T extends CharSequence> Collection<T> getLongWords(Collection<T> coll) [ April 15, 2007: Message edited by: Louis Moloney ]
please excuse my answering you so late, and thanks a lot for your help! I think I'm getting closer now; you're right about my being not so clear about the difference between type (T) and wildcard (?) usage. But now I seem to understand that T is defined in one place only (before the return type) and then serves to establish connections between input and return type. So, in the exam example,
does not really compile when you take the method body into account:
because I don't get a T out of my input collection, and when I try with
it does not really get better because I can't add a CharSequence to a Collection<T> (even though a Collection<T> IS a Collection of things extending CharSequence. So, I conclude it really is the connection established by T usage which counts, not "actual" types. Now, writing all this down it seems rather curious to me I did not get that before... but somehow generics are my hardest topic... Thanks again, Sigrid
Post by:autobot
Our first order of business must be this tiny ad:
a bit of art, as a gift, the permaculture playing cards