比较器 comparable vs comparator

comparable

比较this和其他对象实例,类需实现(implements)java.lang.Comparable接口,重写public int compareTo(Object o1)

comparator

比较2个不同的对象实例,比较器类需实现(implements)java.util.Comparator接口,重写public int compare(Object o1, Object o2)

java.lang.Comparable:int compareTo(Object o1)

比较this Object 和 o1 Object
返回值意义:
1.正数—this Object 大于 o1
2.负数—this Object 小于 o1
3.0—this Object 等于 o1

在排序中,如果当前对象实例this某字段大于 o1某字段,返回正数,即按从小到大排序(正序);

int compareTo(Employee o1){
    if (this.age > o1.age) {
        return 1;
    } else if (this.age < o1.age) {
        return -1;
    } else {
        return 0;
    }
}

java.util.Comparator:int compare(Object o1, Object o2)

比较o1 object 和 o2 object

返回值意义:

1.正数—o1 大于 o2

2.负数—o2 小于 o2

3.0—o1 等于 o2

当o1.getSalary() 大于 o2.getSalary(),返回正数,则从小到大输出(正序);依此类推

//根据对象实例的Salary从小到大
public int compare(Employee o1, Employee o2) {
    // TODO Auto-generated method stub
    if(o1.getSalary() > o2.getSalary()){
        return 1;
    }else if(o1.getSalary() < o2.getSalary()){
        return -1;
    }else{
        return 0;
    }
}

区别:

comparablecomparator
Defined in java.lang.comparableDefinded in java.util.comparator
override public int compareTo(Object o1)override public int compare(Object o1, Object 02)
java.util.Collections.sort(List) and java.util.Arrays.sort(Object[])java.util.Collections.sort(List, Comparator) and java.util.Arrays.sort(Object[], Comparator)
Class whose objects to be sorted must implement this interfaceClass whose objects to be sorted do not need to implement this interface

代码实例:

comparable接口

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

class Human implements Comparable<Human> {

private String name;
private int age;
private double height;

public Human(String name, int age, double height) {
    this.name = name;
    this.age = age;
    this.height = height;
}

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 double getHeight() {
    return height;
}

public void setHeight(double height) {
    this.height = height;
}

// 年龄从小到大
@Override
public int compareTo(Human o) {
    // TODO Auto-generated method stub
    if (this.age > o.age) {
        return 1;
    } else if (this.age < o.age) {
        return -1;
    } else {
        return 0;
    }

    }
}

public class TestHumanSort {

private static void printList(List<Human> list) {
    System.out.println("name\tage\theight");
    for (Human hm : list) {
        System.out.println(hm.getName() + "\t" + hm.getAge() + "\t"
                + hm.getHeight());
    }
}

public static void main(String[] args) {
    List<Human> testHumanList = new ArrayList<Human>();
    testHumanList.add(new Human("Bill", 16, 175));
    testHumanList.add(new Human("John", 18, 170));
    testHumanList.add(new Human("Jack", 15, 180));

    Collections.sort(testHumanList);
    printList(testHumanList);

    }
}

输出:

name    age height
Jack    15  180.0
Bill    16  175.0
John    18  170.0

comparator接口

import java.util.Comparator;

public class EmpSortBySalary implements Comparator<Employee>{

@Override
public int compare(Employee o1, Employee o2) {
    // TODO Auto-generated method stub
    if(o1.getSalary() > o2.getSalary()){
        return 1;
    }else if(o1.getSalary() < o2.getSalary()){
        return -1;
    }else{
        return 0;
    }

    }

}

主函数:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

class Employee{
    private String name;
    private int age;
    private long salary;

    public Employee(String name, int age, long salary){
        this.name = name;
        this.age = age;
        this.salary = salary;
    }
    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 long getSalary() {
        return salary;
    }
    public void setSalary(long salary) {
        this.salary = salary;
    }
}


public class TestEmployeeSort {

    private static void printList(List<Employee> list) {
    System.out.println("name\tage\tsalary");
    for (Employee ey : list) {
        System.out.println(ey.getName() + "\t" + ey.getAge() + "\t"
                + ey.getSalary());
    }
}

public static void main(String[] args) {
    // TODO Auto-generated method stub
    List<Employee> emList = new ArrayList<Employee>();
    emList.add(new Employee("Jack", 28, 10000));
    emList.add(new Employee("John", 60, 3000));
    emList.add(new Employee("Bill", 50, 20000));


    Collections.sort(emList, new EmpSortBySalary());

    printList(emList);


    }

}

输出

name    age salary
John    60  3000
Jack    28  10000
Bill    50  20000
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值