zoukankan      html  css  js  c++  java
  • Comparable和Comparator排序接口

    Comparable和Comparator都是排序的接口,都可以用类来实现,排序效果一样。

    Comparable一般用类来实现,就是大家所说的自然排序;

    Comparator一般用来匿名实现,作为集合的比较器,也就是定制排序;

    Test测类

    package comparatest1;
    
    import java.util.TreeSet;
    /**
     * 
     * @author likainian
     *    1784186573@qq.com
     */
    public class Test {
        public static void main(String[] args) {
            TreeSet<Person>ts=new TreeSet<>();
            Book b1=new Book("a", 12);
            Book b2=new Book("b", 12);
            Book b3=new Book("c", 12);
            Person p1=new Person("a", 1, 1, b1);
            Person p2=new Person("a", 1, 1, b2);
            Person p3=new Person("a", 1, 1, b3);
            ts.add(p1);
            ts.add(p2);
            ts.add(p3);
            System.out.println(ts);
            
        }
    }

    Person类

     1 package comparatest1;
     2 
     3 public class Person implements Comparable<Person>{
     4     private String name;
     5     private int money;
     6     private int age;
     7     private Book book;
     8     public Person(String name, int money, int age, Book book) {
     9         super();
    10         this.name = name;
    11         this.money = money;
    12         this.age = age;
    13         this.book = book;
    14     }
    15     public String getName() {
    16         return name;
    17     }
    18     public void setName(String name) {
    19         this.name = name;
    20     }
    21     public int getMoney() {
    22         return money;
    23     }
    24     public void setMoney(int money) {
    25         this.money = money;
    26     }
    27     public int getAge() {
    28         return age;
    29     }
    30     public void setAge(int age) {
    31         this.age = age;
    32     }
    33     public Book getBook() {
    34         return book;
    35     }
    36     public void setBook(Book book) {
    37         this.book = book;
    38     }
    39     @Override
    40     public int compareTo(Person o) {
    41         int m=-Integer.valueOf(money).compareTo(Integer.valueOf(o.money));
    42         int a=Integer.valueOf(age).compareTo(Integer.valueOf(o.age));
    43         int n=name.compareTo(o.name);
    44         int b=book.compareTo(o.book);
    45         return (m!=0?m:(a!=0?a:(n!=0?n:b)));
    46     }
    47     
    48     @Override
    49     public int hashCode() {
    50         final int prime = 31;
    51         int result = 1;
    52         result = prime * result + age;
    53         result = prime * result + ((book == null) ? 0 : book.hashCode());
    54         result = prime * result + money;
    55         result = prime * result + ((name == null) ? 0 : name.hashCode());
    56         return result;
    57     }
    58     @Override
    59     public boolean equals(Object obj) {
    60         if (this == obj)
    61             return true;
    62         if (obj == null)
    63             return false;
    64         if (getClass() != obj.getClass())
    65             return false;
    66         Person other = (Person) obj;
    67         if (age != other.age)
    68             return false;
    69         if (book == null) {
    70             if (other.book != null)
    71                 return false;
    72         } else if (!book.equals(other.book))
    73             return false;
    74         if (money != other.money)
    75             return false;
    76         if (name == null) {
    77             if (other.name != null)
    78                 return false;
    79         } else if (!name.equals(other.name))
    80             return false;
    81         return true;
    82     }
    83     @Override
    84     public String toString() {
    85         return "Person [name=" + name + ", money=" + money + ", age=" + age
    86                 + ", book=" + book + "]";
    87     }
    88     
    89 }

    Book类

     1 package comparatest1;
     2 
     3 public class Book implements Comparable<Book> {
     4     private String name;
     5     private int price;
     6 
     7     public Book(String name, int price) {
     8         super();
     9         this.name = name;
    10         this.price = price;
    11     }
    12 
    13     public String getName() {
    14         return name;
    15     }
    16 
    17     public void setName(String name) {
    18         this.name = name;
    19     }
    20 
    21     public int getPrice() {
    22         return price;
    23     }
    24 
    25     public void setPrice(int price) {
    26         this.price = price;
    27     }
    28 
    29     @Override
    30     public int compareTo(Book o) {
    31         int p=Integer.valueOf(price).compareTo(Integer.valueOf(o.price));
    32         int n=name.compareTo(o.name);
    33         return (p!=0 ? p : n);
    34     }
    35 
    36     @Override
    37     public int hashCode() {
    38         final int prime = 31;
    39         int result = 1;
    40         result = prime * result + ((name == null) ? 0 : name.hashCode());
    41         result = prime * result + price;
    42         return result;
    43     }
    44 
    45     @Override
    46     public boolean equals(Object obj) {
    47         if (this == obj)
    48             return true;
    49         if (obj == null)
    50             return false;
    51         if (getClass() != obj.getClass())
    52             return false;
    53         Book other = (Book) obj;
    54         if (name == null) {
    55             if (other.name != null)
    56                 return false;
    57         } else if (!name.equals(other.name))
    58             return false;
    59         if (price != other.price)
    60             return false;
    61         return true;
    62     }
    63 
    64     @Override
    65     public String toString() {
    66         return "Book [name=" + name + ", price=" + price + "]";
    67     }
    68 
    69 }
  • 相关阅读:
    IDEA安装和JDK的配置
    IDEA里面创建maven项目,依赖
    JSON的简单书写(初学),使用Java语言反序列化
    Spring中的转换器:Converter
    关于Spring MVC中的表单标签库的使用
    解决Spring框架下中文乱码的问题
    SpringMVC中使用DispatcherServlet
    八大排序算法:冒泡排序
    总线与南桥和北桥
    设备驱动与控制器 I/O
  • 原文地址:https://www.cnblogs.com/String-likainian/p/5824613.html
Copyright © 2011-2022 走看看