#java 中 自定义对象数组排序 Arrays.sort()方法 以及 Compareable接口和Compartor接口对比分析
对自定义对象数组排序,需要引入“比较器”,的概念。
Compareable和Compartor接口就是比较器抽象接口,通过实现类,重写接口方法来进行对象比较。
Arrays.sort()有2个重载方法,见下
当使用sort(Objetc[] a)来进行对象的自然排序,该对象必需实现Compareable接口,重写compareableTo方法,并一般在此方法中定义这3种返回值(1,0,-1)来进行排序标准的确认。
而让对象继成Compareable接口的方式,称为内部比较器。
注意,返回值常规用1,0,-1,并不是说一定要用1或者-1.此处的1和-1只代表俩值相减后为正数还是负数,那么,如果这样理解,我们同样也可以反回 2,0,-2
ps: Collections.sort()方法和Arrays.sort()方法类似,对象也都需要实现Compareable接口。
下面看例
public class Student implements Comparable {
String name;
Integer age;
public Student(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
public Student() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
if (o instanceof Student) {
Student s = (Student) o;
if (this.age > s.age) {
return 1;
} else if (this.age == s.age) {
return 0;
} else {
return -1;
}
}
return 0;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
public class App {
public static void main(String[] args) {
Student s1 = new Student("张一",5);
Student s2 = new Student("张二",3);
Student s3 = new Student("张三",4);
Student s4 = new Student("张四",1);
Student[] stu =new Student[4];
stu[0]=s1;
stu[1]=s2;
stu[2]=s3;
stu[3]=s4;
Arrays.sort(stu);
System.out.println(Arrays.toString(stu));
输出结果
[Student [name=张四, age=1], Student [name=张二, age=3], Student [name=张三, age=4], Student [name=张一, age=5]]
此时我们得到一个age从小到大的结果, 试想,如果我们想要age从大到小的话,那要怎么做呢?
很简单,只需要在compareTo方法中,把return时的1改为-1,-1改为1就成了。
当调用此方法来进行自定义数组排序时,需要我们指定外部比较器。
操作流程为: 定义一个类,如CustomRule,实现Comparator接口,重写其Compare方法 。
那么,CustomRule这种类的定义就是为了给自定义对象排序,它也就是外部比较器。
看例
public class Student2 implements Comparable {
Integer age;
Integer score;
String name;
public Student2() {
super();
// TODO Auto-generated constructor stub
}
public Student2(Integer age, Integer score, String name) {
super();
this.age = age;
this.score = score;
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getScore() {
return score;
}
public void setScore(Integer score) {
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int compareTo(Object o) {
Student2 s = (Student2) o;
if (this.age > s.age) {
return 1;
} else if (this.age == s.age) {
return 0;
} else {
return -1;
}
}
@Override
public String toString() {
return "Student2 [age=" + age + ", score=" + score + ", name=" + name + "]";
}
}
public class CustomRule implements Comparator {
@Override
public int compare(Object o1, Object o2) {
Student2 stu1 = (Student2) o1;
Student2 stu2 = (Student2) o2;
return stu1.score - stu2.score;
}
}
public class App2 {
public static void main(String[] args) {
Student2 s1 = new Student2(5, 50, "张一");
Student2 s2 = new Student2(3, 15, "张二");
Student2 s3 = new Student2(4, 70, "张三");
Student2 s4 = new Student2(1, 23, "张四");
Student2[] stu = new Student2[4];
stu[0] = s1;
stu[1] = s2;
stu[2] = s3;
stu[3] = s4;
Arrays.sort(stu, new CustomRule());
System.out.println(Arrays.toString(stu));
}
}
运算结果
[Student2 [age=3, score=15, name=张二], Student2 [age=1, score=23, name=张四], Student2 [age=5, score=50, name=张一], Student2 [age=4, score=70, name=张三]]
##Compareable接口和Compartor接口对比
说明 :
另外,关于String 中 CompareTo方法总结,可查看本人转载博文 https://blog.csdn.net/ted_cs/article/details/82712248
End!
lalala,我还是我,一个和你一样渴望成为大牛的初级程序员。今天分享结束了。
以上内容如有帮到你,请点赞,评论,收藏。帮我安排一下。