• TreeSet集合排序方式二:定制排序Comparator


    Comparator有两种实现方式:

    1,匿名内部类
    2,创建一个类用于实现Comparator,该类创建的对象就是比较器

    Person类

    public class Person implements Comparable<Person> {
        private String name;
        private String sex;
        private int age;
        private Book books;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getSex() {
            return sex;
        }
        public void setSex(String sex) {
            this.sex = sex;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public Book getBooks() {
            return books;
        }
        public void setBooks(Book books) {
            this.books = books;
        }
        @Override
        public String toString() {
            return "Person [name=" + name + ", sex=" + sex + ", age=" + age + 
                    ", books.name=" + books.getBookName() + ", books.price=" + books.getPrice() + "]";
        }
        public Person(String name, String sex, int age, Book books) {
            super();
            this.name = name;
            this.sex = sex;
            this.age = age;
            this.books = books;
        }
        public Person() {
            super();
        }
    
    }
    

    Book类

    package Homework3;
    
    public class Book {
        //Book类中 有  书名,书的价格
        private String bookName;
        private int 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 + "]";
        }
        public Book(String bookName, int price) {
            super();
            this.bookName = bookName;
            this.price = price;
        }
        public Book() {
            super();
        }
    
    }
    

    Comparator实现–匿名内部类

    public class Test {
        public static void main(String[] args){
            //Comparator
            TreeSet<Person> set2=new TreeSet<>(new Comparator<Person>() {
    
                @Override
                public int compare(Person o1, Person o2) {
                    if(o1.getAge()>o2.getAge()){
                        return -1;
                    }else if (o1.getAge()<o2.getAge()) {
                        return 1;
                    }else {
                        CollationKey key=Collator.getInstance().getCollationKey(o1.getName());
                        CollationKey key2=Collator.getInstance().getCollationKey(o2.getName());
                        int n=key.compareTo(key2);
                        if(n>0){
                            return 1;
                        }else if (n<0) {
                            return -1;
                        }else {
                            int a=o1.getBooks().getPrice()-o2.getBooks().getPrice();
                            if(a>0){
                                return -1;
                            }else if (a<0) {
                                return 1;
                            }else {
                                CollationKey key3=Collator.getInstance().getCollationKey(o1.getBooks().getBookName());
                                CollationKey key4=Collator.getInstance().getCollationKey(o2.getBooks().getBookName());
                                int m=key3.compareTo(key4);
                                if(m>0){
                                    return 1;
                                }else if (m<0) {
                                    return -1;
                                }else {
                                    return 0;
                                }
                            }
    
                        }
                    }
                }
            });
    //          第一条件: 年龄 降序
                set2.add(new Person("Jane1", "female", 20, new Book("z指环王", 30)));
                set2.add(new Person("Jane2", "female", 30, new Book("z指环王", 30)));
    //            第二条件: 姓名升序
                set2.add(new Person("Ada3", "female", 30, new Book("z指环王", 30)));
    //            第三条件: 书的价格   降序
                set2.add(new Person("Ada4", "female", 30, new Book("z指环王", 50)));
    //            第四条件: 书名     升序
                set2.add(new Person("Ada5", "female", 30, new Book("m指环王", 50)));
                System.out.println(set2);
        }
    }
    
  • 相关阅读:
    ListView
    ScrollView-电影列表
    ScrollView
    Image组件
    TextInput
    Touchable类组件
    Text
    View
    FlexBox
    StyleSheet
  • 原文地址:https://www.cnblogs.com/TCB-Java/p/6770141.html
Copyright © 2020-2023  润新知