The difference between comparator interface and comparable interface

Source: Internet
Author: User
Tags comparable

1. comparator  and  Comparable  the same place they are all Java interface,  and is used to compare the size of the custom class, what is the custom class:   such as  PUBLIC CLASS PERSON{ STRING NAME; INT AGE&NBSP,}. When we have such a personlist , which contains the person1, person2, persion3.....,  we use Collections.sort ( personList ),  is not getting the expected results of .  at this time there must be someone to ask,  why you can sort a string list: such as  stringlist{"Hello1"  ,  "Hello3"     "Hello2"}, collections.sort ( stringList )   be able to get the right sort,  that's because  String  This object has helped us implement the  comparable interface  ,  so our  Person  if you want to sort,  also implement a comparator. The difference between    2. Comparator  and  Comparable  comparablecomparable  defined in   Inside of the person class: public class persion implements comparable {. Compare the size of person ..},  because the comparator has been implemented, then our person is now a comparable size object, its comparison function and string is exactly the same, can be used anywhere to compare size, Because person is now the size of their own. Collections.sort (personlist) can get the correct results. comparatorcomparator  is defined outside of the person's, The structure of our person class does not need any change at this time, such as public class person{ string name; int age }, Then we define a different comparator: Public personcomparator implements comparator ()  {. Compare the size of the person:}, in the Personcomparator, implemented how to compare the size of the two man .  so, in this way, when we want to sort a  personlist,  We have to pass personlist in addition to passing the past,  also need to pass the personcomparator, because how to compare the size of the person is implemented in Personcomparator,  such as: Collections.sort ( personlist , new personcomparator ()  ) .3. comparator  and Example of  Comparable  comparable: implementing the comparable interface to overwrite the CompareTo method,  in the CompareTo method to achieve the comparison: Public class  Person implements Comparable {     String name;      int age     public int compareto (Person another )  {          int i = 0;           i = name.coMpareto (Another.name); //  uses a string comparison           if (i  == 0)  { //  If the name is the same, compare the age,  return the comparative age result                 return age - another.age;           } else {                return i; //  name is different,  returns the result of the comparison name .           }     }}    then we can use   directly Collections.sort ( personList )   sort it out. Comparator: Implement Comparator need to cover  compare  method:public class person{      string name;     int age}class personcomparator implements  Comparator {      public int compare (PerSon one, person another)  {          int  i = 0;          i =  One.name.compareTo (another.name); //  use string comparison            if (i == 0)  { //  If the name is the same, compare the age, return the comparison age results                 return one.age - another.age;           } else {                return i; //  name is different,  returns the result of the comparison name .           }     }}   collections.sort (  personlist , new personcomparator ())   can be sorted  4: Summary of the two methods have advantages and disadvantages,  use comparable   Simple,  as long as the implementation comparable&nbspThe object of the interface is directly a comparable object, but need to modify the source code,  The advantage of comparator  is that you do not need to modify the source code,  but also implement a comparator,  when a custom object needs to be compared, Pass the comparator and object together in the past can be compared to the size of the,  and in the comparator  inside the user can implement the complex can be common logic, so that it can match some relatively simple objects, so that can save a lot of duplication of work.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The difference between comparator interface and comparable interface

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.