R
Roger Levy
Having read through Gilad Bracha's tutorial on generics in Java 1.5, I
still don't understand the signature for Collections.max:
public static <T extends Object & Comparable<? super T>> T
max(Collection<? extends T> coll)
The question is, why is the argument for max given type Collection<?
extends T> instead of Collection<T>? In Bracha's tutorial, there's
just one sentence: "max only reads from its input collection, and so
is applicable to collections of any subtype of T".
But it seems to me that given the signature of Collections.max, the
actual value of T will always be indeterminate for a given invocation.
For example, if we have
class A implements Comparable<A> { ... }
class B extends A { ... }
Collection<B> coll = new ArrayList<B>();
// add some B's to coll...
A maximum = Collections.max(coll);
does the T in the signature for max come out to be A or B for this
invocation? It doesn't seem to matter for this particular invocation,
though of course if I had declared maximum to be of type B, then T
would have to come out as B. But all of this would have been fine if
the signature for max took an argument of type Collection<T> rather
than Collection<? extends T>. I imagine I'm not seeing something, so
I would appreciate some edification!
On a related note, I can't find the revised Java Language
Specification for 1.5. Is it available online?
Thanks,
Roger Levy
still don't understand the signature for Collections.max:
public static <T extends Object & Comparable<? super T>> T
max(Collection<? extends T> coll)
The question is, why is the argument for max given type Collection<?
extends T> instead of Collection<T>? In Bracha's tutorial, there's
just one sentence: "max only reads from its input collection, and so
is applicable to collections of any subtype of T".
But it seems to me that given the signature of Collections.max, the
actual value of T will always be indeterminate for a given invocation.
For example, if we have
class A implements Comparable<A> { ... }
class B extends A { ... }
Collection<B> coll = new ArrayList<B>();
// add some B's to coll...
A maximum = Collections.max(coll);
does the T in the signature for max come out to be A or B for this
invocation? It doesn't seem to matter for this particular invocation,
though of course if I had declared maximum to be of type B, then T
would have to come out as B. But all of this would have been fine if
the signature for max took an argument of type Collection<T> rather
than Collection<? extends T>. I imagine I'm not seeing something, so
I would appreciate some edification!
On a related note, I can't find the revised Java Language
Specification for 1.5. Is it available online?
Thanks,
Roger Levy