java比较器 之compareable 和comparato比较
compareable
测试类
import java.util.Set; import java.util.TreeSet; public class Test { public static void main(String[] args) { Set<Person>allset = new TreeSet<Person>(); allset.add(new Person(22,"hjw",20000.0,"论语",50.0)); allset.add(new Person(22,"fyy",7000.0,"大学",40.0)); allset.add(new Person(22,"hjl",6000.0,"孟子",30.0)); allset.add(new Person(22,"hlq",10000.0,"中庸",20.0)); allset.add(new Person(24, "hsx", 20000.0,"诗经", 40.0)); allset.add(new Person(22,"cl",20000.0,"离骚",10.0)); allset.add(new Person(22,"zsk",20000.0,"墨子",25.0)); allset.add(new Person(22,"hjw",20000.0,"论语",23.0)); allset.add(new Person(22,"hjw",20000.0,"论语",22.0)); System.out.println(allset); } }
person类 public class Person extends Book implements Comparable<Person> { private Integer age; private String name; private Double salary; //private String book; public Integer getAge() { return age; } public Person(Integer age, String name, Double salary,String bookname, double price) { super(bookname, price); this.age = age; this.name = name; this.salary = salary; //this.book = book; } public void setAge(Integer age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } @Override public int compareTo(Person o) { if (this.salary>o.salary) { return -1; } else if(this.salary<o.salary){ return 1; } else { if (this.age>o.age){ return 1; }else if(this.age<o.age){ return -1; }else{ if (this.name!=o.name) { return this.name.compareTo(o.name); }else { if(this.price>o.price){ return 1; }else if(this.price<o.price){ return -1; }else { return this.bookname.compareTo(o.bookname); } } } } } @Override public String toString() { return "Person [age=" + age + ", name=" + name + ", salary=" + salary + ", bookname=" + bookname + ", price=" + price + "]"; } }
Book类 public class Book { public String bookname; public Double price; public Book(String bookname, double price) { this.bookname = bookname; this.price = price; } public String getBookname() { return bookname; } public void setBookname(String bookname) { this.bookname = bookname; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } @Override public String toString() { return "Book [bookname=" + bookname + ", price=" + price + "]"; } }
自定义比较器Comparator类
Book类 public class Book { public String bookname; public Double price; public Book(){} public Book(String bookname, double price) { this.bookname = bookname; this.price = price; } public String getBookname() { return bookname; } public void setBookname(String bookname) { this.bookname = bookname; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } @Override public String toString() { return "Book [bookname=" + bookname + ", price=" + price + "]"; } }
Person类 public class Person { private Integer age; private String name; private double salary; private Book book; public Person(Integer age, String name, double salary,String bookname, double price) { this.age = age; this.name = name; this.salary = salary; this.book=new Book(bookname,price); } public Integer getAge() {return age;} public void setAge(Integer age) { this.age = age; } public Book getBook() { return book; } public void setBook(Book book) { this.book = book; } public void setSalary(double salary) { this.salary = salary; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getSalary() { return salary; } @Override public String toString() { return "Person [age=" + age + ", name=" + name + ", salary=" + salary + ", book=" + book + "]"; } }
自定义Comparator类 import java.util.Comparator; public class SalaryCom implements Comparator<Person>{ @Override public int compare(Person o1, Person o2) { if(o1.getSalary()!=o2.getSalary()){ return (int) -(o1.getSalary()-o2.getSalary()); }else if(o1.getAge()!=o2.getAge()){ return (int) (o1.getAge()-o2.getAge()); }else if(o1.getName()!=o2.getName()){ return o1.getName().compareTo(o2.getName());} else{ if(o1.getBook().price!=o2.getBook().price){ return (int) (o1.getBook().price-o2.getBook().price); }else{ return o1.getBook().bookname.compareTo(o2.getBook().bookname); } } } }
Test类 import java.util.Set; import java.util.TreeSet; public class Test { public static void main(String[] args) { SalaryCom s =new SalaryCom(); Set<Person> allset = new TreeSet<Person>(s); allset.add(new Person(22,"hjw",20000,"论语",50.0)); allset.add(new Person(22,"fyy",7000,"大学",40.0)); allset.add(new Person(22,"hjl",6000,"孟子",30.0)); allset.add(new Person(22,"hlq",10000,"中庸",20.0)); allset.add(new Person(24,"hsx",20000,"诗经", 40.0)); allset.add(new Person(22,"cl",20000,"离骚",10.0)); allset.add(new Person(22,"zsk",20000,"墨子",25.0)); allset.add(new Person(22,"hj",20000,"论语",50.0)); allset.add(new Person(22,"hjw",20000,"论语",22.0)); System.out.println(allset); } }
使用匿名内部类自定义比较器Comparator
Book类 public class Book { public String bookname; public Double price; public Book(){} public Book(String bookname, double price) { this.bookname = bookname; this.price = price; } public String getBookname() { return bookname; } public void setBookname(String bookname) { this.bookname = bookname; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } @Override public String toString() { return "Book [bookname=" + bookname + ", price=" + price + "]"; } }
Person类 public class Person { private Integer age; private String name; private double salary; private Book book; public Person(Integer age, String name, double salary,String bookname, double price) { this.age = age; this.name = name; this.salary = salary; this.book=new Book(bookname,price); } public Integer getAge() {return age;} public void setAge(Integer age) { this.age = age; } public Book getBook() { return book; } public void setBook(Book book) { this.book = book; } public void setSalary(double salary) { this.salary = salary; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getSalary() { return salary; } @Override public String toString() { return "Person [age=" + age + ", name=" + name + ", salary=" + salary + ", book=" + book + "]"; } }
Test测试类 import java.util.Comparator; import java.util.Set; import java.util.TreeSet; import com.qianfeng.homework02.Person; public class Test { public static void main(String[] args) { Set<Person> allset = new TreeSet<Person>(new Comparator<Person>() { @Override public int compare(Person o1, Person o2) { if(o1.getSalary()!=o2.getSalary()){ return (int) -(o1.getSalary()-o2.getSalary()); }else if(o1.getAge()!=o2.getAge()){ return (int) (o1.getAge()-o2.getAge()); }else if(o1.getName()!=o2.getName()){ return o1.getName().compareTo(o2.getName());} else{ if(o1.getBook().price!=o2.getBook().price){ return (int) (o1.getBook().price-o2.getBook().price); }else{ return o1.getBook().bookname.compareTo(o2.getBook().bookname); } } } }); allset.add(new Person(22,"hjw",20000,"论语",50.0)); allset.add(new Person(22,"fyy",7000,"大学",40.0)); allset.add(new Person(22,"hjl",6000,"孟子",30.0)); allset.add(new Person(22,"hlq",10000,"中庸",20.0)); allset.add(new Person(24,"hsx",20000,"诗经", 40.0)); allset.add(new Person(22,"cl",20000,"离骚",10.0)); allset.add(new Person(22,"zsk",20000,"墨子",25.0)); allset.add(new Person(22,"hj",20000,"论语",50.0)); allset.add(new Person(22,"hjw",20000,"论语",22.0)); System.out.println(allset.size()); System.out.println(allset); } }
注意:String 字符串类的Comparator已经实现了compareTo方法,可以直接进行调用比较!
善守者藏于九地之下, 善攻者动于九天之上