一、参考
1、【java】Comparator的用法
2、Java 中 Comparable 和 Comparator 比较
二、知识点
1、使用场景:排序、分组
2、使用方法:
2.1 、Arrays.sort(T[],Comparator super T> c);
2.2、 Collections.sort(List
3、区别:
3.1 、Comparator相当于给一个数组或列表新增一种比较方式
3.2 、Comparable是接口需要被类继承的,相当于本身这个数组或者列表及类就有这种比较方式。后面有详细是有案例
三、案例
1、OrderBean订单类
1.1、继承了Comparable这个借口,有个简单的比较,升序的
//订单
public class OrderBean implements Comparable{
private int id; //id
private String cdate; //创建时间
private String product; //产品名称
private int weight; //重量
private long price; //价格
public OrderBean(int id, String cdate, String product, int weight, long price) {
this.id = id;
this.cdate = cdate;
this.product = product;
this.weight = weight;
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCdate() {
return cdate;
}
public void setCdate(String cdate) {
this.cdate = cdate;
}
public String getProduct() {
return product;
}
public void setProduct(String product) {
this.product = product;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public long getPrice() {
return price;
}
public void setPrice(long price) {
this.price = price;
}
@Override
public String toString() {
return "\nOrder_"+id+": ["
+ "cdate="+cdate+", "
+ "product="+product+", "
+ "weight="+weight+" KG, "
+ "price="+price+" RMB, "
+ "]";
}
/**
* 按照weight升序
* sort的话默认调用
* */
public int compareTo(OrderBean o) {
return weight - o.getWeight();
}
}
2、Comparable接口使用
2.1、如果bean类继承Comparable接口,那么它的集合使用Collections.sort(list);可以默认调用bean类中复写的compareTo这个方法,进行排序
2.2、也可以使用compareTo单独比较两个类
private void testComparable() {
System.out.println("\n\n testComparable()>>>");
OrderBean order1 = new OrderBean(1,"2018-01-01","牛肉",10,300);
OrderBean order2 = new OrderBean(5,"2018-01-01","怪兽肉",80,400);
OrderBean order3 = new OrderBean(2,"2018-02-01","牛肉",100,300);
OrderBean order4 = new OrderBean(9,"2018-03-01","唐僧肉",2,600);
List list = new ArrayList();
list.add(order1);
list.add(order2);
list.add(order3);
list.add(order4);
// weight升序排列
Collections.sort(list);
System.out.println("按照订单的weight升序排列:" + list);
System.out.println("比较1和3:"+order1.compareTo(order3));
}
testComparable()>>>
按照订单的weight升序排列:[
Order_9: [cdate=2018-03-01, product=唐僧肉, weight=2 KG, price=600 RMB, ],
Order_1: [cdate=2018-01-01, product=牛肉, weight=10 KG, price=300 RMB, ],
Order_5: [cdate=2018-01-01, product=怪兽肉, weight=80 KG, price=400 RMB, ],
Order_2: [cdate=2018-02-01, product=牛肉, weight=100 KG, price=300 RMB, ]]
比较1和3:-90
3、Comparator比较器的使用
3.1、排序:大于、小于、等于
3.2、分组:等于、不等于
private void testComparator() {
System.out.println("\n\n testComparator()>>>");
OrderBean order1 = new OrderBean(1,"2018-01-01","牛肉",10,300);
OrderBean order2 = new OrderBean(5,"2018-01-01","怪兽肉",80,400);
OrderBean order3 = new OrderBean(2,"2018-02-01","牛肉",100,300);
OrderBean order4 = new OrderBean(9,"2018-03-01","唐僧肉",2,600);
List list = new ArrayList();
list.add(order1);
list.add(order2);
list.add(order3);
list.add(order4);
/**
* ----------------排列-----------------
* 大于、小于、等于
* */
//id降序排列
Collections.sort(list, new Comparator() {
public int compare(OrderBean o1, OrderBean o2) {
return o2.getId() - o1.getId();
}
});
System.out.println("按照订单的id降序排列:"+list);
//单价升序排列
Collections.sort(list, new Comparator() {
public int compare(OrderBean o1, OrderBean o2) {
return (int)(o1.getPrice()/o1.getWeight() - o2.getPrice()/o2.getWeight());
}
});
System.out.println("按照订单的单价升序排列:"+list);
System.out.println("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
/**
* ----------------分组-----------------
* 等于、不等于
* */
List> byDate = divider(list,new Comparator() {
public int compare(OrderBean o1, OrderBean o2) {
return o1.getCdate().equals(o2.getCdate()) ? 0:1;
}
});
for(int i=0;i
testComparator()>>>
按照订单的id降序排列:[
Order_9: [cdate=2018-03-01, product=唐僧肉, weight=2 KG, price=600 RMB, ],
Order_5: [cdate=2018-01-01, product=怪兽肉, weight=80 KG, price=400 RMB, ],
Order_2: [cdate=2018-02-01, product=牛肉, weight=100 KG, price=300 RMB, ],
Order_1: [cdate=2018-01-01, product=牛肉, weight=10 KG, price=300 RMB, ]]
按照订单的单价升序排列:[
Order_2: [cdate=2018-02-01, product=牛肉, weight=100 KG, price=300 RMB, ],
Order_5: [cdate=2018-01-01, product=怪兽肉, weight=80 KG, price=400 RMB, ],
Order_1: [cdate=2018-01-01, product=牛肉, weight=10 KG, price=300 RMB, ],
Order_9: [cdate=2018-03-01, product=唐僧肉, weight=2 KG, price=600 RMB, ]]
\\\\\\\\\\\\\\\\\\
按照订单的cdate分组【0】:[
Order_2: [cdate=2018-02-01, product=牛肉, weight=100 KG, price=300 RMB, ]]
按照订单的cdate分组【1】:[
Order_5: [cdate=2018-01-01, product=怪兽肉, weight=80 KG, price=400 RMB, ],
Order_1: [cdate=2018-01-01, product=牛肉, weight=10 KG, price=300 RMB, ]]
按照订单的cdate分组【2】:[
Order_9: [cdate=2018-03-01, product=唐僧肉, weight=2 KG, price=600 RMB, ]]
分组工具方法
/**
* @author wujn
* @Description:按条件分组
* @param datas
* @param c
* 是否为同一组的判断标准 0 为同一组,1为不同组
* @return
*/
public static List> divider(Collection datas, Comparator super T> c) {
List> result = new ArrayList>();
for (T t : datas) {
boolean isSameGroup = false;
for (int j = 0; j < result.size(); j++) {
if (c.compare(t, result.get(j).get(0)) == 0) {
isSameGroup = true;
result.get(j).add(t);
break;
}
}
if (!isSameGroup) {
// 创建
List innerList = new ArrayList();
result.add(innerList);
innerList.add(t);
}
}
return result;
}