TreeSet中自定义排序的两种方法

* 以此为例子*
人类,属性姓名,收入,年龄,书,
书类:属性书名,价格
存储TreeSet集合,排序规则:
A:先按照收入排序:从高到低
B:收入相等的情况下,按照年龄排序:从小到大
C:如果收入和年龄都相等,按照姓名排序,字典顺序(String类的compareTo方法)
D:如果以上3个属性都相等,按照书排序,先按照书价格:从低到高,如果书价格相等,按照书名(字典顺序)

方法1
//这里我是用的在题目的类中直接继承Comparable 重写了Comparable,TreeSet使用时直接比较了

public class Demo01 {
    public static void main(String[] args) {
        Book book=new Book("java", 20);
        Book book2=new Book("Android", 22);
        Person person=new Person("lcs", 2000, 20, book);
        Person person2=new Person("lcs", 2000, 20, book2);
        Set set=new TreeSet<>();
        set.add(person);
        set.add(person2);
        System.out.println(set);
    }
}
class Book implements Comparable{
    private String bookname;
    private int price;
    public Book(String bookname, int price) {
        super();
        this.bookname = bookname;
        this.price = price;
    }
    public String getBookname() {
        return bookname;
    }
    public void setBookname(String bookname) {
        this.bookname = bookname;
    }
    public int getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }
    @Override
    public String toString() {
        return "Book [bookname=" + bookname + ", price=" + price + "]";
    }
    //按照书排序,先按照书价格:从低到高,如果书价格相等,按照书名(字典顺序)
    @Override
    public int compareTo(Book o) {
        if(this.getPrice()==o.getPrice()){
            return this.getBookname().compareTo(o.getBookname());
        }else if(this.getPrice()return -1;
        }else 
            return 1;
    }

}

class Person implements Comparable{
    //人类,属性姓名,收入,年龄,书
    private String name;
    private int shouru;
    private int age;
    private Book book;
    public Person(String name, int shouru, int age, Book book) {
        super();
        this.name = name;
        this.shouru = shouru;
        this.age = age;
        this.book = book;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getShouru() {
        return shouru;
    }
    public void setShouru(int shouru) {
        this.shouru = shouru;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Book getBook() {
        return book;
    }
    public void setBook(Book book) {
        this.book = book;
    }
    @Override
    public String toString() {
        return "Person [name=" + name + ", shouru=" + shouru + ", age=" + age
                + ", book=" + book + "]";
    }
    /*
     * A:先按照收入排序:从高到低      
        B:收入相等的情况下,按照年龄排序:从小到大
        C:如果收入和年龄都相等,按照姓名排序,字典顺序(String类的compareTo方法)
        D:如果以上3个属性都相等,按照书排序,先按照书价格:从低到高,如果书价格相等,按照书名(字典顺序)
(non-Javadoc)
     * @see java.lang.Comparable#compareTo(java.lang.Object)
     */
    @Override
    public int compareTo(Person o) {
        if(this.getShouru()==o.getShouru()){
            if(this.getAge()==o.getAge()){
                int a=this.getName().compareTo(o.getName());
                if(a==0){
                    return this.book.compareTo(o.book);
                }else if(a<0){
                    return -1;
                }else 
                    return 1;
            }else if(this.getAge()return -1;
            }else
                return 1;
        }else if(this.getShouru()return 1;
        }else 
            return -1;
    }

}

方法2,具体的排序代码和方法1一样,没有具体写出
//这里使用的是定义了一个新的类,实现了comparator的接口,重写了比较的方法,重写关于Person的排序。
//Book的排序已经在Book类中通过实现Comparable接口写好了
//在main方法中这样调用:Set set=new TreeSet<>(new Mycompare());

public class Demo02 {
    public static void main(String[] args) {
        Set set=new TreeSet<>(new Mycompare());
    }
}
class Book implements Comparable{
    private String bookname;
    private int price;
    public Book(String bookname, int price) {
        super();
        this.bookname = bookname;
        this.price = price;
    }
    public String getBookname() {
        return bookname;
    }
    public void setBookname(String bookname) {
        this.bookname = bookname;
    }
    public int getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }
    @Override
    public String toString() {
        return "Book [bookname=" + bookname + ", price=" + price + "]";
    }
    //按照书排序,先按照书价格:从低到高,如果书价格相等,按照书名(字典顺序)  
    @Override
    public int compareTo(Book o) {
        if(this.getPrice()==o.getPrice()){
            return this.getBookname().compareTo(o.getBookname());
        }else if(this.getPrice()return -1;
        }else 
            return 1;
    }
}

class Person{
    //人类,属性姓名,收入,年龄,书
    private String name;
    private int shouru;
    private int age;
    private Book book;
    public Person(String name, int shouru, int age, Book book) {
        super();
        this.name = name;
        this.shouru = shouru;
        this.age = age;
        this.book = book;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getShouru() {
        return shouru;
    }
    public void setShouru(int shouru) {
        this.shouru = shouru;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Book getBook() {
        return book;
    }
    public void setBook(Book book) {
        this.book = book;
    }
    @Override
    public String toString() {
        return "Person [name=" + name + ", shouru=" + shouru + ", age=" + age
                + ", book=" + book + "]";
    }
    /*
     * A:先按照收入排序:从高到低      
        B:收入相等的情况下,按照年龄排序:从小到大
        C:如果收入和年龄都相等,按照姓名排序,字典顺序(String类的compareTo方法)
        D:如果以上3个属性都相等,按照书排序,先按照书价格:从低到高,如果书价格相等,按照书名(字典顺序)
(non-Javadoc)
     * @see java.lang.Comparable#compareTo(java.lang.Object)
     */ 
}
class Mycompare implements Comparator{

    @Override
    public int compare(Person o1, Person o2) {
        // TODO Auto-generated method stub
        return 0;
    }


}

你可能感兴趣的:(Java)