1.java比较器包含Comparable接口和Comparator接口
2.基本使用
package com.yl.pdfdemo.day08.p1;
/**
* @Author wfj
* @Date 2021/6/9
* @Description
* @Version 1.0
*/
public class Goods implements Comparable{
private String name;
private double price;
public Goods() {
}
public Goods(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 "Goods{" +
"name='" + name + '\'' +
", price=" + price +
'}';
}
//比较商品大小,按照价格从低到高,再按照产品名称从高到低排序
@Override
public int compareTo(Object o) {
if (o instanceof Goods) {
Goods goods = (Goods)o;
//方式一:
if (this.getPrice() > goods.getPrice()) {
return 1;
} else if (this.getPrice() < goods.getPrice()) {
return -1;
} else {
return -this.getName().compareTo(goods.getName());
}
}
return 0;
}
}
package com.yl.pdfdemo.day08.p1;
import org.junit.Test;
import java.util.Arrays;
import java.util.Comparator;
/**
* @Author wfj
* @Date 2021/6/9
* @Description 比较器
* @Version 1.0
*/
public class CompareTest {
/**
* java中,对象的比较只有等于或者不等于,不能使用大于等于,少于等于,但是在开发中,我们需要对多个对象进行排序,
* 这时候如何解决?可以使用以下两个接口的任意一个,Comparable 或者 Comparator
*Comparable 与 Comparator
*Comparable接口一旦一定,保证Comparable接口实现类的对象在任何位置都可以比较大小
* Comparator接口属于临时性的比较
*/
/**
* Comparable接口(自然排序)的使用
* 1.像String,包装类等实现了Comparable接口,重写了compareTo(obj)方法,给出了两个对象比较大小的方法
* 2.像String,包装类重写了compareTo(obj)方法,进行了从小到大的排序
* 3.重写compareTo(obj)方法的规则:
* 1)如果当前对象this大于形参对象obj,则返回正整数
* 2)如果当前对象this小于形参对象obj,则返回负整数
* 3)如果当前对象this等于形参obj,则返回零
*/
@Test
public void test1() {
//String重写了compareTo方法
String[] arr = new String[]{"AA","BB","CC","JJ","ZZ","DD","KK"};
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
//[AA, BB, CC, DD, JJ, KK, ZZ]
Goods[] goods = new Goods[6];
goods[0] = new Goods("dell",5000);
goods[1] = new Goods("xiaomi",5666);
goods[2] = new Goods("nokia",1000);
goods[3] = new Goods("hongmi",500);
goods[4] = new Goods("huawei",3600);
goods[5] = new Goods("ai",5000);
Arrays.sort(goods);
System.out.println(Arrays.toString(goods));
//[Goods{name='hongmi', price=500.0}, Goods{name='nokia', price=1000.0}, Goods{name='huawei', price=3600.0}, Goods{name='dell', price=5000.0}, Goods{name='ai', price=5000.0}, Goods{name='xiaomi', price=5666.0}]
}
/**
* Comparator接口(定制排序)的使用
* 1.背景:
* 当元素的类型没有实现java.lang.Comparable接口而又不方便修改代码
* 或者实现了java.lang.Comparable接口的排序规则不适合当前操作
* 那么可以考虑使用Comparator的对象来进行排序
* 2.重写compare(Object o1,Object o2)方法,来比较o1与o2的大小
* 如果方法返回正整数,则表示o1大于o2,
* 如果方法返回负整数,则表示o1小于o2
* 否则,两者相等
*/
@Test
public void test2() {
String[] arr = new String[]{"AA","BB","CC","JJ","ZZ","DD","KK"};
//字符串从大到小排序
Arrays.sort(arr, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
if (o1 instanceof String && o2 instanceof String) {
String s1 = (String)o1;
String s2 = (String)o2;
return -s1.compareTo(s2);
}
return 0;
}
});
System.out.println(Arrays.toString(arr));
Goods[] goods = new Goods[7];
goods[0] = new Goods("dell",5000);
goods[1] = new Goods("xiaomi",5666);
goods[2] = new Goods("nokia",1000);
goods[3] = new Goods("hongmi",500);
goods[4] = new Goods("huawei",3600);
goods[5] = new Goods("ai",5000);
goods[6] = new Goods("hongmi",2000);
//要求,按照商品名称从低到高排序,再到价格从高到低排序
Arrays.sort(goods, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
if(o1 instanceof Goods && o2 instanceof Goods) {
Goods goods1 = (Goods)o1;
Goods goods2 = (Goods)o2;
if (goods1.getName().equals(goods2.getName())) {
return -Double.compare(goods1.getPrice(),goods2.getPrice());
} else {
return goods1.getName().compareTo(goods2.getName());
}
}
return 0;
}
});
System.out.println(Arrays.toString(goods));
}
}