Just went through the implementation of Java 7's java.util.Collections
class, and saw something that I don't understand. In the max
function signature, why is T
bounded by Object
?
public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll) {
Iterator<? extends T> i = coll.iterator();
T candidate = i.next();
while (i.hasNext()) {
T next = i.next();
if (next.compareTo(candidate) > 0)
candidate = next;
}
return candidate;
}
max
seems to work fine if the Object bound is omitted.
public static <T extends Comparable<? super T>> T max(Collection<? extends T> coll) {
Iterator<? extends T> i = coll.iterator();
T candidate = i.next();
while (i.hasNext()) {
T next = i.next();
if (next.compareTo(candidate) > 0)
candidate = next;
}
return candidate;
}
Are there actually any situations where the bound makes a difference? If yes, please provide a concrete example.