TreeSet之用外部比较器实现自定义有序(重要)

 Student.java

package com.sxt.set5;

public class Student{
    private String name;
    private int age;
    private double salary;
    public Student(String name, int age, double salary) {
        super();
        this.name = name;
        this.age = age;
        this.salary = salary;
    }
    public Student() {
        super();
    }
    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 getSalary() {
        return salary;
    }
    public void setSalary(double salary) {
        this.salary = salary;
    }
    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + ", salary=" + salary + "]";
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Student other = (Student) obj;
        if (age != other.age)
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        if (Double.doubleToLongBits(salary) != Double.doubleToLongBits(other.salary))
            return false;
        return true;
    }
    
}

SalarySortOfStudent.java

package com.sxt.set5;

import java.util.Comparator;

/*
 * TreeSet:有序
 * implements Comparator<Student> 
 * 如果用外部比较器TreeSet必须是实现Comparator接口来实现有序
 * 使用泛型是为了在重写compare()方法时,object不用强制转换类型
 */

//外部比较器:在新建类内重写比较规则即compare()方法
public class SalarySortOfStudent implements Comparator<Student>{

    //外部比较器的自定义比较规则:按照salary排序
    @Override
    public int compare(Student o1, Student o2) {
        //double类型的比较:不能直接写成return o1.getSalary-o2.getSalary
        //因为方法重写 返回值类型为基本数据类型必须一致  所以返回double会转成int会损失精度
        if(o1.getSalary() < o2.getSalary()){//降序
            return 1;
        }else{
            return -1;//不能是0即不能返回相等 否则结果会少输出 因为Set接口是(无序,唯一)
        }
    }
    
}

NameSortOfStudent.java

package com.sxt.set5;

import java.util.Comparator;

/*
 * 外部比较器:名字相同时 按照年龄降序输出
 * 实现Comparator  重写比较规则即compare()方法
 */


public class NameSortOfStudent implements Comparator<Student>{

    @Override
    public int compare(Student stu1, Student stu2) {
        if(stu1.getName().equals(stu2.getName())){
            return stu2.getAge() - stu1.getAge();
        }
        return stu1.getName().compareTo(stu2.getName());
        //String类已经实现了Comparable接口 已经重写了compareTo()方法
    }
    
}

TestStudent.java

package com.sxt.set5;
import java.util.HashSet;
/*
 * TreeSet 有序的 二叉树(红黑数)
 */
import java.util.Set;
import java.util.TreeSet;

public class TestStudent {
    public static void main(String[] args) {
        //排序之比较规则
        SalarySortOfStudent ss = new SalarySortOfStudent();
        TreeSet<Student> arr = new TreeSet<>(ss);//ss:外部比较器即比较规则
        arr.add(new Student("bbb", 21, 532.2));
        arr.add(new Student("ccc", 32, 32.2));
        arr.add(new Student("ddd", 11, 352.2));
        arr.add(new Student("aaa", 15, 32.2));
        //遍历
        System.out.println("按照外部比较器按薪资降序排列:");
        for(Student s:arr){
            System.out.println(s);
        }
//        按照外部比较器按薪资(double)降序排列 结果为:
//        Student [name=bbb, age=21, salary=532.2]
//        Student [name=ddd, age=11, salary=352.2]
//        Student [name=aaa, age=15, salary=32.2]
//        Student [name=ccc, age=32, salary=32.2]
        
        System.out.println("----------------------------------------");
        
        NameSortOfStudent ns = new NameSortOfStudent();
        TreeSet<Student> arr1 = new TreeSet<>(ns);//ns:外部比较器即比较规则
        arr1.add(new Student("bbb", 21, 532.2));
        arr1.add(new Student("ccc", 32, 32.2));
        arr1.add(new Student("aaa", 11, 352.2));
        arr1.add(new Student("aaa", 15, 32.2));
        System.out.println("按照外部比较器按名字排序(如果名字相同则按年龄降序排序):");
        for(Student s:arr1){
            System.out.println(s);
        }
//        按照外部比较器按名字排序(如果名字相同则按年龄降序排序):
//        Student [name=aaa, age=15, salary=32.2]
//        Student [name=aaa, age=11, salary=352.2]
//        Student [name=bbb, age=21, salary=532.2]
//        Student [name=ccc, age=32, salary=32.2]

    }
}

 

posted @ 2017-04-21 17:36  清风追梦enjoy  阅读(1232)  评论(0编辑  收藏  举报