比较器Comparable与Comparator实践

比较器Comparable与Comparator实践

Comparable:该比较器是用于对象的内部比较,比较的是属性。它的方法:

 public int compareTo(T o);

Comparator:该比较器用于对象的外部比较,比较的是对象,一般这个比较的对象也实现了Comparable接口。Comparator的方法有多个,但一般我们只需要实现:

int compare(T o1, T o2);

实例代码:

声明一个People类,实现Comparable接口,内部排序优先级,city>age>name>sex。


public class People implements Comparable<People> {
    public String name;
    public int sex;
    public String city;
    public int age;

    public People(String name, int sex, String city, int age) {
        this.name = name;
        this.sex = sex;
        this.city = city;
        this.age = age;
    }

    @Override
    public String toString() {
        return "People{" +
                "name='" + name + '\'' +
                ", sex=" + sex +
                ", city='" + city + '\'' +
                ", age=" + age +
                '}';
    }

    /**
     * 比较规则优先级:city>age>name>sex
     */
    @Override
    public int compareTo(People people) {

        int result = 0;

        if (people == null) return result;

        //比较city
        result = people.city.compareTo(city);//String实现了Comparable接口

        if (result == 0) {//city 相同
            result = people.age - age;//比较age
        }

        if (result == 0) {//   age相同
            result = people.name.compareTo(name);//比较name
        }

        if (result == 0) { //name相同
            result = people.sex - sex;//比较sex
        }

        return result;
    }
}

实现一个Comparator比较器

public class PeopleComparator implements Comparator<People> {
    @Override
    public int compare(People people1, People people2) {
        if (people1 != null && people2 != null) {
            return people2.compareTo(people1);//后者和前者比较
        }
        return 0;
    }
}

测试代码实现:

public class Test {
    public static void main(String[] args) {
        List<People> list = new ArrayList<>();

        list.add(new People("moly", 1, "shanghai", 24));
        list.add(new People("lili", 1, "beijing", 25));
        list.add(new People("pop", 1, "shenzhen", 24));
        list.add(new People("momo", 0, "shanghai", 23
        ));

        System.out.println(list.toString());

        Collections.sort(list,new PeopleComparator());

        System.out.println(list.toString());
    }
}

打印结果:

sort before:[People{name='moly', sex=1, city='shanghai', age=24}, People{name='lili', sex=1, city='beijing', age=25}, People{name='pop', sex=1, city='shenzhen', age=24}, People{name='momo', sex=0, city='shanghai', age=23}]

sort after:[People{name='lili', sex=1, city='beijing', age=25}, People{name='momo', sex=0, city='shanghai', age=23}, People{name='moly', sex=1, city='shanghai', age=24}, People{name='pop', sex=1, city='shenzhen', age=24}]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值