简体   繁体   English

泛型的解释<T extends Comparable <? superT >>在collection.sort /可比代码?

[英]Explanation of generic <T extends Comparable<? super T>> in collection.sort/ comparable code?

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. 基本上如果我有一个人类,我会得到它来实现Comparable接口,并将提供compareTo的实现。 However in the definition of Collections.sort in javadocs, I see this signature 但是在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? 有人可以帮我弄这个吗?

Actually, it means that T can implement Comparable<? super T> 实际上,这意味着T 可以实现Comparable<? super T> Comparable<? super T> , not just Comparable<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> ,其中StudentPerson的子类:

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实例作为Person传递给compareTo() (当然,除非你将它转发)。

In practice however, you'll never see a Student class implement Comparable<Person> . 但实际上,您永远不会看到Student类实现Comparable<Person> That's because Person will probably have implemented Comparable<Person> , and Student inherits it implementation. 那是因为Person可能已经实现了Comparable<Person> ,而Student继承了它的实现。 The end result is the same however: you can pass a List<Student> to Collections.sort() and have it sorted on Person 's properties. 最终结果是相同的:您可以将List<Student>传递给Collections.sort()并将其排序在Person的属性上。

The difference between Comparable<T> and Comparable<? super T> Comparable<T>Comparable<? super T>之间的区别Comparable<? super T> Comparable<? super T> is more obvious in the overloaded version of Collections.sort() that takes a Comparator<? super T> Comparable<? super T>Collections.sort()的重载版本中更明显,它带有一个Comparator<? super T> Comparator<? 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());

You always use extends with generics wildcards, even if the type parameter implements an interface. 即使type参数实现了接口,也始终使用带有泛型通配符的extends

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 Comparable interface and how it's used in the compareTo() method. 如果考虑传递给Comparable接口的类型参数以及它在compareTo()方法中的使用方式,这是有意义的。

As PM 77-1 has eloquently pointed out, the super keyword allows for either the class, T, or one of its parents to implement Comparable . 正如PM 77-1雄辩地指出的那样, super关键字允许类,T或其父类之一实现Comparable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM