刚刚通过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;
}
实际上是否存在边界产生影响的情况?如果是,请提供一个具体的例子。