java Comparable和Comparator用法详解

1.接口的方法

Comparable接口内只有一个方法:

public interface Comparable<T> {
    public int compareTo(T o);
}

Comparator接口内方法挺多,具体看API文档

2.比较方式

Comparable用于集合内对象的自动比较,需在自定义类中重写compareTo方法。往集合内添加对象后,集合便会对其排序,一般在TreeSet集合内使用较多。

举例:

import java.util.TreeSet;
//Comparable实例
public class Demo {
    public static void main(String[] args) {
        TreeSet<Person> data = new TreeSet<Person>();
        Person p1 = new Person("张三", 28);
        Person p2 = new Person("李四", 22);
        Person p3 = new Person("王五", 27);
        data.add(p1);
        data.add(p2);
        data.add(p3);
        for (Person person : data) {
            System.out.println(person);
        }
    }
    static class Person implements Comparable<Person>{
        private String name;
        private int age;
        @Override
        public int compareTo(Person p) {
            // this 与 p 比较
            //若比较年龄,则直接比较数值的大小,此为升序
            return this.age-p.getAge();
            //若比较字符串,则可调用String自带的CompareTo方法来比较
            //return this.name.compareTo(p.getName());
        }

        //set以及一些其他方法忽略不写了


        public String getName() {
            return name;
        }
        public int getAge() {
            return age;
        }

        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }
        public Person() {
            super();
        }

        @Override
        public String toString() {
            return "Person [name=" + name + ", age=" + age + "]";
        }
    }
}

Comparator用法:在集合外部使用 Collections.sort(参数1,参数2) 对集合进行排序,参数1为要排序的集合,参数2为排序方式的对象。

注意:Collections.sort() 只能对List集合进行排序

举例:

public static void main(String[] args) {
        List<People> list = new ArrayList<People>();
        People p1 = new People("张三", 25);
        People p2 = new People("李四", 22);
        People p3 = new People("王五", 27);
        list.add(p1);
        list.add(p2);
        list.add(p3);
        Mycompare order = new Mycompare();
        Collections.sort(list, order);

        System.out.println(list);
    }

//People类
class People{
    private String name;
    private int age;

    public int getAge() {
        return age;
    }

    public People(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    @Override
    public String toString() {
        return "People [name=" + name + ", age=" + age + "]";
    }
}

//自定义比较类
public class Mycompare implements Comparator<People>{
    @Override
    public int compare(People p1, People p2) {
        if(p1.getAge() > p2.getAge()) {
            return 1;
        }else if(p1.getAge() == p2.getAge()) {
            return 0;
        }
        return -1;
    }
}

这样定义三个类有些麻烦,因为其中的一个实现Comparator类完全没必要,可使用匿名对象的方法简化一下代码。

public class Demo {
    public static void main(String[] args) {
        List<People> list = new ArrayList<People>();
        People p1 = new People("张三", 25);
        People p2 = new People("李四", 22);
        People p3 = new People("王五", 27);
        list.add(p1);
        list.add(p2);
        list.add(p3);
        Collections.sort(list, new Comparator<People>() {
            @Override
            public int compare(People o1, People o2) {
                return o1.getAge()-o2.getAge();
            }
        });
        System.out.println(list);
    }
}
// People类省略了

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页