欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

treeSet集合中的自然排序和比较器排序

程序员文章站 2022-07-09 22:34:38
...

Set接口类有三个子类:HashSet,LinkedList和TreeSet;
TreeSet底层数据结构是二叉树和哈希表,所以能对元素进行排序,且元素唯一;

treeSet排序分为两种:

  1. 自然排序(使用空参构造):需要集合中的元素实现Comparable接口,并且重写接口中的compareTo方法;
  2. 比较器排序(使用有参构造):传入一个比较器并实现它(可以定义一个类,实现比较器这个接口);另外一般采用匿名内部类的方式传入,比较简单;
//自然排序:

//测试类:
public class Test {
    public static void main(String[] args) {
        TreeSet<Student> set = new TreeSet<>();
        set.add(new Student("zhangsan", 19));
        set.add(new Student("zhangsan", 19));
        set.add(new Student("lisi", 20));
        set.add(new Student("wangwu", 22));
        set.add(new Student("lisi", 18));
        for (Student student : set) {
            System.out.println(student);
        }
    }
}
//Student类:
class Student implements Comparable<Student>//实现Comparable这个接口
    private String name;
    private int age;
    public Student(){}
    public Student(String name, int age){
        this.name = name;
        this.age = age;
    }

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

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
    @Override
    public int compareTo(Student student) {//重写compareTo方法
        int num1=this.age-student.age;
        int num2=num1==0?(this.name.compareTo(student.name)):num1;
        return num2;
//方法返回值的正,负和0 ,来决定此元素在二叉树中放置的左右顺序,返回0此元素就不会往里面放置;
    }
}

//比较器排序:

//Student类:
class Student {
    private String name;
    private int age;
    public Student(){}
    public Student(String name, int age){
        this.name = name;
        this.age = age;
    }

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

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

}
//测试类:
public class Test {
    public static void main(String[] args) {
        TreeSet<Student> set = new TreeSet<>(new Comparator<Student>() {
        //采用匿名内部类传入比较器对象;
            @Override
            public int compare(Student s1, Student s2) {
                int num1=s1.getAge()-s2.getAge();
                int num2=num1==0?(s1.getName().compareTo(s2.getName())):num1;
                return num2;

            }
        });
        set.add(new Student("zhangsan", 19));
        set.add(new Student("zhangsan", 19));
        set.add(new Student("lisi", 20));
        set.add(new Student("wangwu", 22));
        set.add(new Student("lisi", 18));
        for (Student student : set) {
            System.out.println(student);
        }
    }
}


//测试类还可以这样写,只是比较复杂:
public class Test {
    public static void main(String[] args) {
        TreeSet<Student> set = new TreeSet<>(myComparator);
        set.add(new Student("zhangsan", 19));
        set.add(new Student("zhangsan", 19));
        set.add(new Student("lisi", 20));
        set.add(new Student("wangwu", 22));
        set.add(new Student("lisi", 18));
        for (Student student : set) {
            System.out.println(student);
        }
    }
}

class myComparator implements Comparator<Student>{
   public int compare(Student s1, Student s2) {
         int num1=s1.getAge()-s2.getAge();
         int num2=num1==0?(s1.getName().compareTo(s2.getName())):num1;
         return num2;
            }
}

另外,比较器亦可对integer型数组和集合进行排序:

//1.数组:
public class Test1 {
    public static void main(String[] args) {
       Integer[] arr={12,23,1,9,43,2,14,8,5,23};
        Arrays.sort(arr, new Comparator<Integer>() {
            //调用数组工具类Arrays中的排序方法,传入数组和比较器进行排序;
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1-o2;
            }
        });
        System.out.println(Arrays.toString(arr));
    }
}

//2. 集合

public class Test1 {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(12);
        list.add(53);
        list.add(43);
        list.add(33);
        list.add(3);
        list.add(21);
        list.add(73);
        list.add(63);
        list.add(2);
        list.sort(new Comparator<Integer>() {
        //集合对象调用排序sort方法,传入比较器,即可对元素进行排序;
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1-o2;
            }
        });
        System.out.println(list);
    }
}