Comparable vs Comparator

  • Comparable interface can be used to provide single way of sorting whereas Comparator interface is used to provide different ways of sorting.
  • For using Comparable, Class needs to implement it whereas for using Comparator we don’t need to make any change in the class.
  • Comparable interface is in java.lang package whereas Comparator interface is present in java.utilpackage.
  • We don’t need to make any code changes at client side for using Comparable, Arrays.sort() or Collection.sort() methods automatically uses the compareTo() method of the class. For Comparator, client needs to provide the Comparator class to use in compare() method.

 

复制代码
public class Employee implements Comparable<Employee> {

    private Long id;

    private String name;

    private Double salary;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getSalary() {
        return salary;
    }

    public void setSalary(Double salary) {
        this.salary = salary;
    }

    public Employee() {
    }

    public Employee(Long id, String name, Double salary) {
        this.id = id;
        this.name = name;
        this.salary = salary;
    }

    @Override
    public int compareTo(Employee o) {
        return (int) (this.id - o.id);
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this)
                .append("id", id)
                .append("name", name)
                .append("salary", salary)
                .toString();
    }
}
复制代码
复制代码
public static void main(String[] args) {

        List<Employee> employeeList = new ArrayList<>();
        employeeList.add(new Employee(13L, "hello", 8000D));
        employeeList.add(new Employee(17L, "world", 10000D));
        employeeList.add(new Employee(13L, "test", 12000d));
        employeeList.add(new Employee(14L, "demo", 15000D));

        Collections.sort(employeeList);

        for (Employee e : employeeList) {

            System.out.println(e);
        }

        Collections.sort(employeeList, new Comparator<Employee>() {
            @Override
            public int compare(Employee o1, Employee o2) {
                int flag = (int) (o1.getId() - o2.getId());
                if (flag == 0) {
                    flag = (int) (o2.getSalary() - o1.getSalary());
                }

                return flag;
            }
        });

        System.out.println("comparator sort:");

        for (Employee e : employeeList) {

            System.out.println(e);
        }


    }
复制代码
复制代码
Employee@4d76f3f8[id=13,name=hello,salary=8000.0]
Employee@2d8e6db6[id=13,name=test,salary=12000.0]
Employee@23ab930d[id=14,name=demo,salary=15000.0]
Employee@4534b60d[id=17,name=world,salary=10000.0]
comparator sort:
Employee@2d8e6db6[id=13,name=test,salary=12000.0]
Employee@4d76f3f8[id=13,name=hello,salary=8000.0]
Employee@23ab930d[id=14,name=demo,salary=15000.0]
Employee@4534b60d[id=17,name=world,salary=10000.0]
复制代码

 

posted @   parkdifferent  阅读(315)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示