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方法,可以直接进行调用比较!
java比较器 之compareable 和comparato比较的更多相关文章
- Java比较器对数组,集合排序一
数组排序非常简单,有前辈们的各种排序算法,再加上Java中强大的数组辅助类Arrays与集合辅助类Collections,使得排序变得非常简单,如果说结合比较器Comparator接口和Collato ...
- java比较器Comparable接口和Comaprator接口
Comparable故名思意是比较,意思就是做比较的,然后进行排序. 1.什么是comparable接口 此接口强行对实现它的每个类的对象进行整体排序.此排序被称为该类的自然排序 ,类的 compar ...
- 黑马----JAVA比较器:Comparable和Comparator
黑马程序员:Java培训.Android培训.iOS培训..Net培训 一.Comparable接口 1.public interface Comparable{ public int compare ...
- Java中equals与==和comparaTo的区别
一.先说说Java中equals和==的区别: Java中的数据类型,可分为两类: 1.基本数据类型(也叫原始数据类型) 八大基本数据类型 char byte short int long doubl ...
- java比较器Comparator 使用
PresonDemo package cn.stat.p5.person.demo; public class PresonDemo implements Comparable { private S ...
- Java比较器
导语 本节内容,比较器Comparable是核心内容. 主要内容 重新认识Arrays类 两种比较器的使用 具体内容 Arrays类 在之前一直使用的"java.util.Arrays.so ...
- Java 比较器
比较器 Arrays 类 主要功能: 完成所有与数组有关的操作的工具类 二分查找: 在一个有序的数字序列中进行二分查找 public static int binarySearch(数据类型 [] a ...
- TreeSet的两种实现方法:Comparable和Comparator(Java比较器)
Comparable与Comparator实际上是TreeSet集合的两种实现方式,用来实现对象的排序.下边介绍一下两种比较器的使用方法和区别. Comparable称为元素的自然顺序,或者叫做默认顺 ...
- 小白养成记——Java比较器Comparable和Comparator
一.使用情景 1. 调用Arrays.sort()方法或Collections.sort()方法对自定义类的对象排序 以Arrays.sort()为例.假定有如下自定义的Person类 1 publ ...
随机推荐
- SQL Server 引起磁盘IO的原因
1. 要访问的数据不在内存中.这会引起IO 2. update/delete/insert 会把变化写入数据文件.还要记录日志 : 3. checkpoint 4. lazy writer 5. db ...
- 一个失误导致微信下载图片接口Token失效
公司的应用调了一个微信上传下载图片的接口,本来在线上跑的好好的,什么问题没有,但是这两天总是不定时的出现下载下来的图片损坏,拿着Token和serverid去接口测试网页验证,返回的是Token失效了 ...
- interbase C++Builder 简单例子
interbase C++Builder 的例子,网上找了半天也没找到合适的,下面是一般能搜索到的文章,现在整理下: 下面我以interbase―――C++Builder,介绍一个简单的例子(不过很 ...
- android 自定义AlertDialog(一段)
java: final AlertDialog dialog = new AlertDialog.Builder(mContext) .create(); dialog.setCancelable(f ...
- HDU 4082 Hou Yi's secret(暴力)
直接6重循环就行了吧...判三角形相似直接从小到大枚举两向量夹角是否相等就行了.注意去重点跟三点共线就行了... #include<algorithm> #include<iostr ...
- boost::asio 使用 libcurl
curl 使用 asio 的官方样例 http://curl.haxx.se/libcurl/c/asiohiper.html, 但这个例子用起来有很明细的 bug,asio 异步IO 只注册一次,也 ...
- poj1163 dp入门
The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 36811 Accepted: 22048 De ...
- Another attempt about LSI
Last week I was here Natural Language Processing in NZ. Someone asked a question, is there any exist ...
- python 魔法方法之:__getitem__ __setitem__ __delitem__
h2 { color: #fff; background-color: #7CCD7C; padding: 3px; margin: 10px 0px } h3 { color: #fff; back ...
- 【Quick-COCOS2D-X 3.3 怎样绑定自己定义类至Lua之四】使用绑定C++至Lua的自己定义类
续[Quick-COCOS2D-X 3.3 怎样绑定自己定义类至Lua之三]动手绑定自己定义类至Lua 之后.我们已经完毕了自己定义类至Lua的绑定.在接下来的环节,我们将使用它. 首先,我们须要确定 ...