为什么 T 在集合.max() 签名中受对象限制?
刚刚浏览了Java 7类的实现,看到了一些我不明白的东西。在函数签名中,为什么受 ?java.util.Collections
max
T
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
如果省略对象绑定,似乎工作正常。
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;
}
实际上是否有任何绑定会有所作为的情况?如果有,请举一个具体例子。