Java比较器
Java比较器 实现对象排序
自然排序:java.lang.Comparable接口
定制排序:java.util.Comparator接口
Comparable接口
Comparable接口的使用举例:
1、像String、包装类等实现了Comparable接口,重写了compareTo(obj)方法,给出了比较两个对象大小的方式
2、像String、包装类重写compareTo()方法以后,进行了从小到大的排列
String[] arr = new String[]{"aa", "hh", "zz", "kk", "ee", "cc"};
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
[aa, cc, ee, hh, kk, zz]
3、重写compareTo(obj)的规则:
如果当前对象this大于形参对象obj,则返回正整数
如果当前对象this小于形参对象obj,则返回负整数
如果当前对象this等于形参对象obj,则返回0
4、对于自定义类,如果需要排序,需要实现Comparable接口,重写compareTo(obj)方法。
自定义商品类实现Comparable自然排序
/**
* 商品类
*/
public class Product implements Comparable {
private String name;
private double price;
public Product() {
}
public Product(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Product{" +
"name='" + name + '\'' +
", price=" + price +
'}';
}
//按商品价格从低到高排序,价格相同则按名称字母从前到后排序(A-Z,a-z)
@Override
public int compareTo(Object o) {
if (this == o) {
return 0;
} else if (o instanceof Product) {
Product product = (Product) o;
int value = Double.compare(this.price, product.price);
if (value != 0) {
return value;
} else {
return this.name.compareTo(product.name);
}
}
throw new RuntimeException("数据类型不一致!");
}
}
//对Product商品类进行价格排序
Product[] products = new Product[6];
products[0] = new Product("Xiaomi13Pro", 4999);
products[1] = new Product("RedmiK50Pro", 2999);
products[2] = new Product("iPhone14ProMax", 9999);
products[3] = new Product("VivoX90", 4999);
products[4] = new Product("HuaweiMate50Pro", 6999);
products[5] = new Product("Honor90Pro", 3999);
//实现接口重写方法前: Products cannot be cast to java.lang.Comparable
// Arrays.sort(products);
Arrays.sort(products);
for (Product product : products) {
System.out.println(product.toString());
}
排序结果:
Product{name='RedmiK50Pro', price=2999.0}
Product{name='Honor90Pro', price=3999.0}
Product{name='VivoX90', price=4999.0}
Product{name='Xiaomi13Pro', price=4999.0}
Product{name='HuaweiMate50Pro', price=6999.0}
Product{name='iPhone14ProMax', price=9999.0}
Comparator接口
定制排序Comparator
Comparator接口的使用:
1、背景:当元素的类型没有实现Comparable接口而又不方便修改代码,或者实现了Comparable接口的排序规则不适合当前的操作,那么可以考虑使用Comparator的对象来排序。
2、重写compare(Object o1,Object o2)方法,比较o1,o2的大小。
如果方法返回正整数,表示o1大于o2;如果返回0,表示相等,如果返回负整数,表示o1小于o2。
//String数组Comparator按字母从大到小排序
Arrays.sort(arr, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
if (o1 instanceof String && o2 instanceof String) {
String s1 = (String) o1;
String s2 = (String) o2;
return -s1.compareTo(s2);
}
// return 0;
throw new RuntimeException("输入的数据类型不一致!");
}
});
System.out.println(Arrays.toString(arr));
排序结果:
[zz, kk, hh, ee, cc, aa]
Product[] products = new Product[6];
products[0] = new Product("Xiaomi13Pro", 4999);
products[1] = new Product("RedmiK50Pro", 2999);
products[2] = new Product("iPhone14ProMax", 9999);
products[3] = new Product("VivoX90", 4999);
products[4] = new Product("HuaweiMate50Pro", 6999);
products[5] = new Product("Honor90Pro", 3999);
//实现价格从高到低排序,字母从A到Z排序
Comparator comparator = new Comparator() {
@Override
public int compare(Object o1, Object o2) {
if (o1 instanceof Product && o2 instanceof Product) {
Product p1 = (Product) o1;
Product p2 = (Product) o2;
int value = -Double.compare(p1.getPrice(), p2.getPrice());
if(value != 0){
return value;
}else {
return p1.getName().compareTo(p2.getName());
}
}
throw new RuntimeException("数据类型不一致!");
}
};
Arrays.sort(products,comparator);
for (Product product : products) {
System.out.println(product.toString());
}
Product{name='iPhone14ProMax', price=9999.0}
Product{name='HuaweiMate50Pro', price=6999.0}
Product{name='VivoX90', price=4999.0}
Product{name='Xiaomi13Pro', price=4999.0}
Product{name='Honor90Pro', price=3999.0}
Product{name='RedmiK50Pro', price=2999.0}
Comparable接口和Comparator接口的使用对比
Comparable自然排序:单一的,唯一的,一劳永逸,对应抽象方法compareTo(Object obj)
Comparator定制排序:灵活的,多样的,临时的,对应抽象方法compare(Object o1, Object o2)
Comparable接口的方式一旦指定,保证Comparable接口实现类的对象在任何位置都可以比较大小,Comparator接口属于临时性的比较。