通用< T扩展可比较<的解释super T>>在collection.sort /可比较的代码? [英] Explanation of generic <T extends Comparable<? super T>> in collection.sort/ comparable code?
问题描述
我一直使用可比的界面,通过collection.sort为我的课程提供自然排序。
基本上,如果我有一个人课程,我会把它实现Comparable接口并将提供compareTo的实现。然而,在javadocs的Collections.sort定义中,我看到了这个签名
public static< T extends Comparable< ;? super T>> void sort(List< T> list)
我完全不理解这个泛型定义吗?不应该只是说
< T实现了Comparable< T>>
有人可以帮助我吗?
实际上,它意味着T 可以实现 Comparable<超级T>
,而不仅仅是可比< T>
。 例如,它意味着 Student
类可以实现 Comparable< Person>
,其中 Student
是 Person的一个子类:
public class Person { }
public class Student extends Person implements Comparable< Person> {
@Override public int compareTo(Person that){
// ...
}
}
在这种情况下,List可以按 Collections.sort()
排序,但只能基于 Person
的属性,因为您将 Student
实例传递给 compareTo()
作为一个 Person
(除非你沮丧,当然)。
实际上,你永远不会看到学生
类实现可比< Person>
。这是因为 Person
可能会实现 Comparable< Person>
,并且 Student
继承它的实现。但最终结果是一样的:您可以将 List< Student>
传递给 Collections.sort()
它对 Person
的属性进行了排序。
$ b Comparable< T>
和可比较<?超级T>
在带有 Comparator< ;?的Collections.sort()
的重载版本超级T> :
class ByAgeAscending实现比较器< Person> {
@Override public int compare(Person a,Person b){
return a.getAge()< b.getAge();
}
}
列表< Student> students = getSomeStudents();
Collections.sort(students,new ByAgeAscending());
I use comparable interface all the time to provided natural ordering for my class through 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
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?
Actually, it means that T can implement Comparable<? super T>
, not just Comparable<T>
.
For example, it means that a Student
class can implement Comparable<Person>
, where Student
is a subclass of 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).
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.
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>
:
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());
这篇关于通用< T扩展可比较<的解释super T>>在collection.sort /可比较的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!