Java is like this-Comparator (Comparable, Comparator)

Source: Internet
Author: User

Sometimes you need to compare Collection objects or arrays that are not a single number. There are two methods: 1 is to implement the Comparable interface and 2 is to implement the Comparator interface. 1. the ComParable interface Comparable interface is defined as follows: public interface Comparable <T> {public int compareTo (T o);} This method returns an int type of data, however, the value of this int can only be the following three types: 1: greater than-1: smaller than 0: equal instance: package zy. java. lang; import java. util. arrayList; import java. util. arrays; import java. util. collections; import java. util. list; public class ComparableTest implements Comparable <ComparableTest> {private String id; private int age; public ComparableTest (S Tring id, int age) {this. id = id; this. age = age;} public String getId () {return id;} public void setId (String id) {this. id = id;} public int getAge () {return age;} public void setAge (int age) {this. age = age ;}@ Override public int compareTo (ComparableTest c) {return this. age-c. getAge ();} public static void main (String [] args) {// Collection ComparableTest ct1 = new ComparableTest ("", 39); ComparableTest ct2 = new ComparableTest ("B", 31); ComparableTest ct3 = new ComparableTest ("c", 38 ); list <ComparableTest> list = new ArrayList <ComparableTest> (); list. add (ct1); list. add (ct2); list. add (ct3); Collections. sort (list); for (int I = 0; I <list. size (); I ++) {System. out. println (list. get (I ). getId ();} System. out. println ("-------------------------------------------"); // Array ComparableTest [] Ct = new ComparableTest [] {new ComparableTest ("a", 39), new ComparableTest ("B", 31), new ComparableTest ("c", 38 )}; arrays. sort (ct); for (int I = 0; I <ct. length; I ++) {System. out. println (ct [I]. getId () ;}}} 2. to solve this problem, if a class has been opened, but the Comparable interface is not implemented at the initial stage of this class, object sorting cannot be performed, java defines the Comparator operation interface of another Comparator. This interface is defined in java. in the util package, the interface is defined as follows: public interface Comparator <T> {public int compare (T o1, T o2);} instance: package zy. java. lang; public class Users {private String id; private int age; public Users (String id, int age) {this. id = id; this. age = age;} public String getId () {return id;} public void setId (String id) {this. id = id;} public int getAge () {return age;} public void setAge (int age) {this. age = age ;}} 12345678910111213141516171819202122232425262728293031323334353637383940414 2434445464748 package zy. java. lang; import java. util. *; public class ComparatorTest implements Comparator <Users >{@ Override public int compare (Users o1, Users o2) {return o1.getAge ()-o2.getAge ();} public static void main (String [] agrs) {// Collection Users u1 = new Users ("a", 33); Users u2 = new Users ("B", 31 ); users u3 = new Users ("c", 38); List <Users> list1 = new ArrayList <Users> (); list1.add (u1 ); List1.add (u2); list1.add (u3); Collections. sort (list1, new ComparatorTest (); for (int I = 0; I <list1.size (); I ++) {System. out. println (list1.get (I ). getId ();} System. out. println ("---------------------------------------"); // Array Users [] u = new Users [] {new Users ("a", 39), new Users ("B", 31 ), new Users ("c", 38)}; Arrays. sort (u); for (int I = 0; I <u. length; I ++) {System. out. println (u [I]. getId ());}}} Select the Comparable interface or Comparator? When a class implements the Comparable interface, it indicates that the objects of this class can be compared with each other. A set composed of these class objects can be directly sorted using the sort method. Comparator can be seen as an algorithm implementation that separates algorithms from data. Comparator can also be used in the following two environments: 1. Category designers did not consider the comparison problem and did not implement Comparable, you can use Comparator to sort objects without changing the object itself. 2. You can use multiple sorting criteria, such as ascending or descending.

Related Article

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.