【问题标题】:What is the link between Collections.sort method with the overriden methods of Comparable and Comparator Interface?Collections.sort 方法与 Comparable 和 Comparator 接口的重写方法之间有什么联系?
【发布时间】:2016-08-02 16:00:45
【问题描述】:

为了对对象集合进行排序,我们有 Comparable 和 Collection 接口,因此我们覆盖了对象存储在集合中的类中接口的相应方法。 但是在排序时,我们调用 Collections.sort(List) 和 Collections.sort(List,Comparator) ,Collections 类的静态方法来对集合中的对象进行排序。所以我需要知道这个排序方法和我们重写的接口方法之间的联系是什么。排序方法内部是否调用了这些重写的接口方法-compareTo/compare?

【问题讨论】:

标签: collections


【解决方案1】:

我明白了为什么在对象集合的情况下需要比较器,但我只需要知道 compareTo 和 compare 方法是如何调用的? 我们实际上是在调用 Collections.sort 方法并将其传递给 List 和/或比较器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-29
    • 2020-06-24
    • 1970-01-01
    • 2013-01-28
    • 1970-01-01
    相关资源
    最近更新 更多