Java record-69-difference between Comparable and Comparator

Source: Internet
Author: User

Java record-69-difference between Comparable and Comparator
Comparable & Comparator are used to compare and sort elements in a set, but Comparable is the sorting implemented by the methods defined inside the set, and Comparator is the sorting implemented outside the set. Therefore, to achieve sorting, You need to define the Comparator interface method outside the set or implement the Comparable interface method in the set. It can be said that the comparison is done by yourself, and the comparison is made by external programs. Comparator is located under the java. util package, while Comparable is located under the java. lang package. ComparableComparable is an interface that the object itself supports for self-comparison (for example, the String and Integer operations can be done by itself to compare the size, and the Comparable interface has been implemented) A custom class can be sorted after being added to the list container. You can implement the Comparable interface. If you do not specify a Comparator when using the sort method of the Collections class, sort by natural order, as the API says: Sorts the specified list into ascending order, according to the natural ordering of its elements. all elements in the list must implement the Comparable interface the natural order here is the sorting method set by the Comparable interface. Comparator and Comparator are dedicated Comparator. When this object does not support auto-comparison or the auto-comparison function cannot meet your requirements, you can write a comparator to compare the sizes of two objects. Using Comparator is a strategy design pattern. Instead of changing the object itself, a strategy object is used to change its behavior. For example, if you want to sort Integers by absolute values, Integer does not meet the requirements. You do not need to modify the Integer class (you cannot actually do this) to change its sorting behavior, you only need to use an object that implements the Comparator interface to control its sorting.

Public class AbsComparator implements Comparator {public int compare (Object o1, Object o2) {int v1 = Math. abs (Integer) o1 ). intValue (); int v2 = Math. abs (Integer) o2 ). intValue (); return v1> v2? 1: (v1 = v2? 0:-1) ;}} public class Test {public static void main (String [] args) {// generates an array of 20 random integers (positive and negative) random rnd = new Random (); Integer [] integers = new Integer [20]; for (int I = 0; I <integers. length; I ++) integers [I] = new Integer (rnd. nextInt (100) * (rnd. nextBoolean ()? 1:-1); System. out. println ("sort by Integer built-in method:"); Arrays. sort (integers); System. out. println (Arrays. asList (integers); System. out. println ("sorted by AbsComparator:"); Arrays. sort (integers, new AbsComparator (); System. out. println (Arrays. asList (integers ));}}

 

Comparator and Comparable provide the following methods: Comparator defines two methods: int compare (T o1, T o2) and boolean equals (Object obj ), used to compare whether two Comparator are equal true only if the specified object is also a comparator and it imposes the same ordering as this comparator. sometimes the equals method is not implemented when the Comparator interface is implemented, but the program does not report an error because the class implementing this interface is also a subclass of the Object class, the Object class has implemented the equals method Comparable interface and only provides the int compareTo (T o) method. Use Comparator and Comparable: If I define a Person class, and when the Person class implements the Comparable interface, after instantiating the Person class's person1, to compare the size of person1 and an existing Person object person2, I can call it in this way. The returned value can be used to determine: person1.comparTo (person2 ); when a PersonComparator interface is defined, you can: PersonComparator Comparator = new PersonComparator (); comparator. compare (person1, person2 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.