java 泛型 <T extends Comparable<? 的解释 超级 T>> 在 collection.sort/ 可比代码中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25779184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Explanation of generic <T extends Comparable<? super T>> in collection.sort/ comparable code?
提问by Dude
I use comparable interface all the time to provided natural ordering for my class through collection.sort.
我一直使用可比较的接口通过 collection.sort 为我的班级提供自然排序。
Basically if I have a person class, I will get it to implement Comparable interface and will provide the implementation of compareTo. However in the definition of Collections.sort in javadocs, I see this signature
基本上,如果我有一个 person 类,我会让它实现 Comparable 接口并提供 compareTo 的实现。但是在 javadocs 中 Collections.sort 的定义中,我看到了这个签名
public static <T extends Comparable<? super T>> void sort(List<T> list)
I don't understand this generics definition at all? Shouldn't it just say
我根本不明白这个泛型定义?不应该只是说
<T implements Comparable<T>>
Can someone help me with this?
有人可以帮我弄这个吗?
回答by Jan Van den bosch
Actually, it means that T canimplement Comparable<? super T>
, not just Comparable<T>
.
实际上,这意味着 T可以实现Comparable<? super T>
,而不仅仅是Comparable<T>
。
For example, it means that a Student
class can implement Comparable<Person>
, where Student
is a subclass of Person
:
例如,这意味着一个Student
类可以实现Comparable<Person>
,其中Student
是 的子类Person
:
public class Person {}
public class Student extends Person implements Comparable<Person> {
@Override public int compareTo(Person that) {
// ...
}
}
In this case, a List can be sorted by Collections.sort()
but only based on Person
's properties, because you pass the Student
instance into compareTo()
as a Person
(unless you downcast it, of course).
在这种情况下,一个 List 可以排序,Collections.sort()
但只能基于Person
的属性,因为您将Student
实例compareTo()
作为 a传递给Person
(当然,除非您向下转换它)。
In practice however, you'll never see a Student
class implement Comparable<Person>
. That's because Person
will probably have implemented Comparable<Person>
, and Student
inherits it implementation. The end result is the same however: you can pass a List<Student>
to Collections.sort()
and have it sorted on Person
's properties.
然而,在实践中,您永远不会看到Student
类 implement Comparable<Person>
。那是因为Person
will 可能已经实现了Comparable<Person>
,并Student
继承了它的实现。然而,最终结果是相同的:您可以将 a 传递List<Student>
给Collections.sort()
并根据Person
的属性对其进行排序。
The difference between Comparable<T>
and Comparable<? super T>
is more obvious in the overloaded version of Collections.sort()that takes a Comparator<? super T>
:
在Collections.sort() 的重载版本中,Comparable<T>
和之间的区别Comparable<? super T>
更加明显,它采用:Comparator<? super T>
class ByAgeAscending implements Comparator<Person> {
@Override public int compare(Person a, Person b) {
return a.getAge() < b.getAge();
}
}
List<Student> students = getSomeStudents();
Collections.sort(students, new ByAgeAscending());
回答by jahroy
You always use extendswith generics wildcards, even if the type parameter implements an interface.
您总是使用带有泛型通配符的扩展,即使类型参数实现了一个接口。
If you look at a class that implements Comparable, you'll see that it actually (should) implement Comparable<T>
, where T is the class itself.
如果您查看实现Comparable的类,您会发现它实际上(应该)实现Comparable<T>
,其中 T 是类本身。
It makes sense if you think about the type paramter passed to the Comparableinterface and how it's used in the compareTo()method.
如果您考虑传递给Comparable接口的类型参数以及它在compareTo()方法中的使用方式,这很有意义。
As PM 77-1 has eloquently pointed out, the superkeyword allows for either the class, T, or one of its parents to implement Comparable.
正如 PM 77-1 雄辩地指出的那样,super关键字允许类 T 或其父类之一实现Comparable。