Java中Comparator接口与Comparable接口区别

Java中Comparator接口与Comparable接口

引用头文件

Comparator位于包java.util下,而Comparable位于包 java.lang下

区别

Comparable & Comparator 都是用来实现集合中元素的比较、排序的,只是 Comparable 是在集合内部定义的方法实现的排序,Comparator 是在集合外部实现的排序,所以,如想实现排序,就需要在集合外定义 Comparator 接口的方法或在集合内实现 Comparable 接口的方法。

comparable

当要排序时需呀需要重写Comparable接口,重写方法compareTo();

在这里插入代码片
public class StudentAsc implements Comparable<StudentAsc> {
    private String name;
 
    private Integer age;
 
    public StudentAsc(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
 
    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;
    }
 
    public int compareTo(StudentAsc o) {
        if(null == this.age) {
            return -1;
        }
        if(null == o.getAge()) {
            return 1;
        }
        return this.age.compareTo(o.getAge());
    }
 
    @Override
    public String toString() {
        return "StudentAsc{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
 
}

调用

在这里插入代码片
//正序排序,年龄为null时为小
StudentAsc studentWang = new StudentAsc("王小二", 10);
StudentAsc studentZhang = new StudentAsc("张三", 1);
StudentAsc studentGou = new StudentAsc("狗子", 99);
StudentAsc studentZhao = new StudentAsc("赵六", 40);
StudentAsc studentLi = new StudentAsc("李四", null);
List<StudentAsc> studentAscs = new ArrayList<StudentAsc>(Arrays.asList(studentWang, studentZhang, studentGou, studentZhao, studentLi));
Collections.sort(studentAscs);
System.out.println("自定义对象,升序排序:");
for(StudentAsc studentAsc : studentAscs) {
    System.out.println(studentAsc.toString());
}

//正序排序,年龄为null时为小
StudentAsc studentWang = new StudentAsc("王小二", 10);
StudentAsc studentZhang = new StudentAsc("张三", 1);
StudentAsc studentGou = new StudentAsc("狗子", 99);
StudentAsc studentZhao = new StudentAsc("赵六", 40);
StudentAsc studentLi = new StudentAsc("李四", null);
List<StudentAsc> studentAscs = new ArrayList<StudentAsc>(Arrays.asList(studentWang, studentZhang, studentGou, studentZhao, studentLi));
Collections.sort(studentAscs);
System.out.println("自定义对象,升序排序:");
for(StudentAsc studentAsc : studentAscs) {
    System.out.println(studentAsc.toString());
}



代码结束

Comparator方法

    person [] arr={
            new person("xiao a",18),
            new person("xiao b",20)
    };  //person类数组
 //   Comparator<person> comparator = new comparator<person>() ;
  Arrays.sort(arr, new Comparator<person>() { //调用Comarator函数
       @Override
       public int compare(person o1, person o2) {
           return o1.getAge()-o2.getAge();
       }
   });

或者用mycomparator实现Comparator接口

public class mycomparator implements Comparator<person> {
    @Override
    public int compare(person o1, person o2) {
        return o1.getAge()-o2.getAge();
    }
}

在这里插入代码片

调用

person [] arr={
new person(“xiao a”,28),
new person(“xiao b”,20)
};
System.out.println(arr);
// Comparator comparator = new comparator() ;
Arrays.sort(arr, new mycomparator());
``

总结

comparator和comprable都是两种实现自定义排序的接口。
comparable需要实现comparaTo方法
comparator需要实现接口Comparator 重写compare方法

发布了8 篇原创文章 · 获赞 0 · 访问量 1210

猜你喜欢

转载自blog.csdn.net/qq_42695315/article/details/104031293