Deep analysis of Java comparator comparable interface and Comaprator interface _java

Source: Internet
Author: User
Tags arrays comparable stub java comparator
The Java comparator has two classes, namely the comparable interface and the comparator interface .
When sorting an array of objects, the comparator works very clearly, first of all, to explain the comparable interface.
Let the object that needs to be sorted implement the comparable interface, override the CompareTo (T O) method in which you define the collation, and then call Java.util.Arrays.sort () directly to sort the array of objects, as follows:
Copy Code code as follows:

Class Student implements comparable<student>{
private String name;
private int age;
private float score;

Public Student (String name, int age, float score) {
THIS.name = name;
This.age = age;
This.score = score;
}

Public String toString ()
{
return name+ "\t\t" +age+ "\t\t" +score;
}
@Override
public int compareTo (Student o) {
TODO auto-generated Method Stub
if (This.score>o.score)//score is private, why can it be called directly, because within the student class
return-1;//by the high end sort
else if (This.score<o.score)
return 1;
else{
if (this.age>o.age)
Return 1;//sort from bottom to high
else if (this.age<o.age)
return-1;
Else
return 0;
}
}
}
public class ComparableDemo01 {
/**
* @param args
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
Student stu[]={new Student ("Zhangsan", 20,90.0f),
New Student ("Lisi", 22,90.0f),
New Student ("Wangwu", 20,99.0f),
New Student ("Sunliu", 22,100.0f)};
Java.util.Arrays.sort (Stu);
for (Student S:stu)
{
System.out.println (s);
}
}
}

program Run Result:
Sunliu 22 100.0
Wangwu 20 99.0
Zhangsan 20 90.0
Lisi 22 90.0
But when designing a class, often does not consider lets the class implement the comparable interface, then we need to use another comparator interface comparator.
From the above example, we can see that compareTo (t o) has only one parameter, and the Compare (T o1,t O2) that must be implemented in the comparator interface has two parameters.
Code instance:
Copy Code code as follows:

Package edu.sjtu.ist.comutil;
Import Java.util.Comparator;
Class Student {
private String name;
private int age;
private float score;

Public Student (String name, int age, float score) {
THIS.name = name;
This.age = age;
This.score = score;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
public int getage () {
return age;
}
public void Setage (int age) {
This.age = age;
}
public float Getscore () {
return score;
}
public void SetScore (float score) {
This.score = score;
}
Public String toString ()
{
return name+ "\t\t" +age+ "\t\t" +score;
}
}
Class Studentcomparator implements comparator<student>{
@Override
public int Compare (Student O1, Student O2) {
TODO auto-generated Method Stub
if (O1.getscore () >o2.getscore ())
return-1;
else if (O1.getscore () <o2.getscore ())
return 1;
else{
if (O1.getage () >o2.getage ())
return 1;
else if (O1.getage () <o2.getage ())
return-1;
Else
return 0;
}
}

}
public class ComparableDemo02 {
/**
* @param args
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
Student stu[]={new Student ("Zhangsan", 20,90.0f),
New Student ("Lisi", 22,90.0f),
New Student ("Wangwu", 20,99.0f),
New Student ("Sunliu", 22,100.0f)};
Java.util.Arrays.sort (Stu,new studentcomparator ());
for (Student S:stu)
{
System.out.println (s);
}
}
}

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.