赞
踩
在Java中,我们会经常使用到自定义类,那我们如何进行自定义类的比较呢?
普通数据的比较
- int a=10;
- int b=91;
- System.out.println(a<b);
那自定义类型可不可以这样比较呢?看一下代码
我们发现会报错,因为自定义类型,stu1和stu2里面存的是引用,是无法直接根据姓名或年龄进行比较的。
如果想要自定义类型根据年龄和名字进行比较,这时候就要用到我们的Comparable接口。
当我们观察Comparable接口的底层细节会发现有一个<T>和一个方法,<T>代表我们要比较的类型,方法是我们根据实际情况来重写compareTo方法,也就是比较的规则。
自定义类中具体实现
- class Student implements Comparable<Student>{
- public String name;
- public int age;
-
- public Student(String name, int age) {
- this.name = name;
- this.age = age;
- }
-
- @Override
- public int compareTo(Student o) {
- //根据年龄比较
- /*if(this.age>o.age){
- return 1;
- }else if (this.age==o.age){
- return 0;
- }else {
- return -1;
- }*/
- return this.age-o.age;
- }
- }

完整代码
- class Student implements Comparable<Student>{
- public String name;
- public int age;
-
- public Student(String name, int age) {
- this.name = name;
- this.age = age;
- }
-
- @Override
- public int compareTo(Student o) {
- //根据年龄比较
- /*if(this.age>o.age){
- return 1;
- }else if (this.age==o.age){
- return 0;
- }else {
- return -1;
- }*/
- return this.age-o.age;
- }
- }
- public class Test {
- public static void main(String[] args) {
- Student stu1=new Student("zhansan",18);
- Student stu2=new Student("man",24);
- System.out.println(stu1.compareTo(stu2));
- }
- }

- class Student implements Comparable<Student>{
- public String name;
- public int age;
-
- public Student(String name, int age) {
- this.name = name;
- this.age = age;
- }
-
- @Override
- public int compareTo(Student o) {
- return this.name.compareTo(o.name);
- }
- }
- public class Test {
- public static void main(String[] args) {
- Student stu1=new Student("zhansan",18);
- Student stu2=new Student("man",24);
- System.out.println(stu1.compareTo(stu2));
- }
- }

由于名字是String类,String类在底层中也实现了compareTo方法,所以我们可以直接调用compareTo方法来实现名字的比较。
多个对象我们可以用一个对应类的数组来存储,然后思路就是让数组里面的元素就行比较。
这里模拟了冒泡排序进行比较。
根据名字来排序
- import java.util.Arrays;
-
- class Student implements Comparable<Student>{
- public String name;
- public int age;
-
- public Student(String name, int age) {
- this.name = name;
- this.age = age;
- }
-
- @Override
- public String toString() {
- return "Student{" +
- "name='" + name + '\'' +
- ", age=" + age +
- '}';
- }
-
- @Override
- public int compareTo(Student o) {
- return this.name.compareTo(o.name);
- }
- }
- public class Test {
- public static void mysort(Comparable[] comparables){
- for (int i = 0; i < comparables.length-1; i++) {
- for(int j=0;j<comparables.length-1-i;j++){
- if(comparables[j].compareTo(comparables[j+1])>0){
- Comparable tmp=comparables[j];
- comparables[j]=comparables[j+1];
- comparables[j+1]=tmp;
- }
- }
- }
- }
- public static void main(String[] args) {
- Student[] students=new Student[]{
- new Student("zhansan",18),
- new Student("man",24),
- new Student("lebron",23)
- };
- mysort(students);
- System.out.println(Arrays.toString(students));
-
- }
- }

根据年龄来排序
- import java.util.Arrays;
-
- class Student implements Comparable<Student>{
- public String name;
- public int age;
-
- public Student(String name, int age) {
- this.name = name;
- this.age = age;
- }
-
- @Override
- public String toString() {
- return "Student{" +
- "name='" + name + '\'' +
- ", age=" + age +
- '}';
- }
-
- @Override
- public int compareTo(Student o) {
- return this.age-o.age;
- }
- }
- public class Test {
- public static void mysort(Comparable[] comparables){
- for (int i = 0; i < comparables.length-1; i++) {
- for(int j=0;j<comparables.length-1-i;j++){
- if(comparables[j].compareTo(comparables[j+1])>0){
- Comparable tmp=comparables[j];
- comparables[j]=comparables[j+1];
- comparables[j+1]=tmp;
- }
- }
- }
- }
- public static void main(String[] args) {
- Student[] students=new Student[]{
- new Student("zhansan",18),
- new Student("man",24),
- new Student("lebron",23)
- };
- mysort(students);
- System.out.println(Arrays.toString(students));
-
- }
- }

1.当前阶段如果我们想要进行自定义类型之间的比较,我们要使用Comparable接口。
2.重写接口里面的方法是我们根据需求来决定如何重写compareTo方法,重写后的compareTo方法里面的具体实现就是我们的比较规则。
我们发现当我们使用Comparable接口时并不是那么灵活,因为它实现的比较规则是写死的,如果我们想要换一种比较规则,我们必须要对实现对比较方法里面的重新构造。
那有没有比较灵活的比较方式呢?答案就是Comparator接口。
AgeComparator类
- public class AgeComparator implements Comparator<Student> {
- @Override
- public int compare(Student o1, Student o2) {
- return o1.age- o2.age;
- }
- }
NameComparator类
- public class NameComparator implements Comparator<Student> {
- @Override
- public int compare(Student o1, Student o2) {
- return o1.name.compareTo(o2.name);
- }
- }
主函数部分
根据年龄排序
- public class Test {
- public static void main(String[] args) {
- Student[] students=new Student[]{
- new Student("zhansan",18),
- new Student("man",24),
- new Student("lebron",23)
- };
- NameComparator nameComparator=new NameComparator();
- AgeComparator ageComparator=new AgeComparator();
- Arrays.sort(students,ageComparator);
- System.out.println(Arrays.toString(students));
-
- }
- }
根据名字比较
- public class Test {
- public static void main(String[] args) {
- Student[] students=new Student[]{
- new Student("zhansan",18),
- new Student("man",24),
- new Student("lebron",23)
- };
- NameComparator nameComparator=new NameComparator();
- AgeComparator ageComparator=new AgeComparator();
- Arrays.sort(students,nameComparator);
- System.out.println(Arrays.toString(students));
-
- }
- }
这里我们定义了AgeComparator类和NameComparator类,它们都使用了Comparator这个接口,
然后在自己的类里面重写了compareTo方法。
根据以上类实现的对象可以认为是比较规则,将这些对象作为sort函数的参数,就可以灵活实现不同比较方式的转变。
相对于Comparable接口来说,Comparator不需要改变函数内部的具体实现来改变比较规则,只需改变函数的参数就行了,这样更安全也更方便。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。