java的Comparable和Comparator接口使用(二)

这里主要介绍Comparator

Comparator和Comparable接口功能相似,用法有区别

通过name的length比较大小

@Data
public class Employeee {
    private String name;
    private double salary;

    public Employeee(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    public static void main(String[] args) {
        Employeee[] staff = new Employeee[3];
        staff[0] = new Employeee("Sun Wukong", 130000);
        staff[1] = new Employeee("Pig Bajie", 11000);
        staff[2] = new Employeee("Sha Wujing", 12000);
        Arrays.sort(staff, new Comparator<Employeee>() {
            @Override
            public int compare(Employeee o1, Employeee o2) {
                return o1.name.length()-o2.name.length();
            }
        });
        for(Employeee employee:staff){
            System.out.println(employee.name+"::"+employee.salary);
        }
    }
}
也可以使用lambda表达式
@Data
public class Employeee {
    private String name;
    private double salary;

    public Employeee(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }
    public static void main(String[] args) {
        Employeee[] staff = new Employeee[3];
        staff[0] = new Employeee("Sun Wukong", 130000);
        staff[1] = new Employeee("Pig Bajie", 11000);
        staff[2] = new Employeee("Sha Wujing", 12000);
        Arrays.sort(staff, (o1,o2)->o1.name.length()-o2.name.length());
        for(Employeee employee:staff){
            System.out.println(employee.name+"::"+employee.salary);
        }
    }
}

也可以用Comparator.comparing( Function<? super T, ? extends U> keyExtractor)方法

@Data
public class Employeee {
    private String name;
    private double salary;

    public Employeee(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }
    public static void main(String[] args) {
        Employeee[] staff = new Employeee[3];
        staff[0] = new Employeee("Sun Wukong", 130000);
        staff[1] = new Employeee("Pig Bajie", 11000);
        staff[2] = new Employeee("Sha Wujing", 12000);
        Arrays.sort(staff,Comparator.comparing(Employeee::getName));
        for(Employeee employee:staff){
            System.out.println(employee.name+"::"+employee.salary);
        }
    }
}

而且可以对多个属性比较,如果第一次比较相同,就比较第二个属性,这里桑Employeee同name,那么就比较salary

public class Employeee {
    private String name;
    private double salary;

    public Employeee(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }
    public static void main(String[] args) {
        Employeee[] staff = new Employeee[3];
        staff[0] = new Employeee("Pig Bajie", 130000);
        staff[1] = new Employeee("Pig Bajie", 11000);
        staff[2] = new Employeee("Pig Bajie", 12000);
        Arrays.sort(staff,Comparator.comparing(Employeee::getName).thenComparing(Employeee::getSalary));
        for(Employeee employee:staff){
            System.out.println(employee.name+"::"+employee.salary);
        }
    }
}

还有一种使用Comparator的方法,让类直接implements Comparator<>

但是这种方法的泛型括号里不能写本类,而是比较的类型属性就是什么类型,比如比较name长度就是implements Comparator<String>,因为需要Arrays.sort(names,Comparator.comparing(String::length));的括号里的两个属性是同一种类型。

如果是本类,但是要比较类里面的属性,就做不到,只能如下这种:

public class LengthComparator implements Comparator<String> {

    @Override
    public int compare(String o1, String o2) {
        return o1.length() - o2.length();
    }

    public static void main(String[] args) {
        String[] names = {"Sun Wukong", "Pig Bajie", "Sha Wujing"};
        //Arrays.sort(names, new LengthComparator());

        //Arrays.sort(names, (first,second)->first.length()-second.length());
        Arrays.sort(names,Comparator.comparing(String::length));
        for(String name:names){
            System.out.println(name);
        }
    }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值