Difference between Comparable and Comparator?

后端 未结 3 1505
忘了有多久
忘了有多久 2021-01-21 04:07

My understand is Comparator can compare multiple objects from different classes while Comparable can only compare one onject with another instance in the same class.

C

3条回答
  •  天命终不由人
    2021-01-21 04:10

    This answer is based on the information extracted from the Complete Java SE 8 Developer Bootcamp course by Intertech Training. According to them

    The Comparable interface says “This particular object is gonna be compared to another object” whereas the Comparator is kind of like this third-party “give me any two objects and I’ll tell you how they’re ordered”.

    Both Comparable and Comparator are located in java.util.package.

    Strings, charts, ints, and so on all have a “natural ordering” (following the unicode, so the highest precedence in ordering would be numbers, then capital letters and lower case letters). If we’re going to sort custom classes, then we’ll have to implement a Comparable interface to help the sorting methods understand what they should do (to get Collections or Arrays to sort your types, the types must implement the Comparable interface).

    A class implementing the Comparable interface must implement the compareTo() method. This method takes a generic Object. For instance,

    public class MyDate implements Comparable {
        private int day, month, year;
        public MyDate(int month, int day, int year) {
            this.day = day;
            this.year = year;
            this.month = month;
        }
        public int compareTo(Object o) {
            MyDate d = (MyDate) o;
            if (year != d.year) {
                return year - d.year;
            }
            if (month != d.month ) {
                return month - d.month;
            }
            if (day != d.day) {
                return day - d.day;
            }
            return 0;
        }
        ...
    }
    

    A class implementing the Comparator interface must implement the compare() method. This method takes two Objects as arguments and returns a positive number if the first argument is larger than the second, zero if they are equal, and a negative number otherwise.

    public class MyDate implements Comparator {
        private int day, month, year;
        public MyDate(int month, int day, int year) {
            this.day = day;
            this.year = year;
            this.month = month;
        }
        public int compare(Object o1, Object o2) {
            MyDate d1 = (MyDate) o1;
            MyDate d2 = (MyDate) o2;
            if (d1.year != d2.year) {
                return d1.year - d2.year;
            }
            if (d1.month != d2.month ) {
                return d1.month - d2.month;
            }
            if (d1.day != d2.day) {
                return d1.day - d2.day;
            }
            return 0;
        }
        ...
    }
    

    A Comparator can be used to compare any two objects. When comparing like objects, it is best to use the Comparable interface when possible.

    The Comparator is useful when comparing elements of a heterogeneous collection and third-party classes we don’t have the source to.

提交回复
热议问题