First, reference
II. Knowledge points
1. Usage scenarios: sorting and grouping
2. Usage:
2.1 ,Arrays.sort(T[],Comparator<? super T> c);
2.2, Collections.sort(List<T> list,Comparator<? super T> c);
3. Difference:
3.1. Comparator is equivalent to adding a comparison method to an array or list
3.2. Comparable is an interface that needs to be inherited by a class, which is equivalent to the array, list and class itself. There are detailed cases in the back
Three, case
1. OrderBean order class
1.1. Inheriting the excuse of Comparable, there is a simple comparison, ascending
//Order public class OrderBean implements Comparable<OrderBean>{ private int id; //id private String cdate; //Creation time private String product; //Product name private int weight; //weight private long price; //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, " + "]"; } /** * In ascending order of weight * sort Call by default * */ public int compareTo(OrderBean o) { return weight - o.getWeight(); } }
2. Use of compatible interface
2.1. If the bean class inherits the compatible interface, its collection uses Collections.sort(list); by default, it can call the replicated compareTo method in the bean class to sort
2.2. You can also use compareTo to compare two classes separately
private void testComparable() { System.out.println("\n\n testComparable()>>>"); OrderBean order1 = new OrderBean(1,"2018-01-01","beef",10,300); OrderBean order2 = new OrderBean(5,"2018-01-01","Monster meat",80,400); OrderBean order3 = new OrderBean(2,"2018-02-01","beef",100,300); OrderBean order4 = new OrderBean(9,"2018-03-01","Tang Monk meat",2,600); List<OrderBean> list = new ArrayList<OrderBean>(); list.add(order1); list.add(order2); list.add(order3); list.add(order4); // weight ascending Collections.sort(list); System.out.println("According to the order weight Ascending:" + list); System.out.println("Compare 1 and 3:"+order1.compareTo(order3)); }
testComparable()>>> In ascending order of order weight:[ Order_9: [cdate=2018-03-01, product = Tang Seng meat, weight=2 KG, price=600 RMB,], Order_1: [cdate=2018-01-01, product = beef, weight=10 KG, price=300 RMB,], Order_5: [cdate=2018-01-01, product = monster meat, weight=80 KG, price=400 RMB,], Order_2: [cdate=2018-02-01, product = beef, weight=100 KG, price=300 RMB,]] Compare 1 and 3: - 90
3. Use of Comparator
3.1. Sorting: greater than, less than, equal to
3.2. Grouping: equal to, not equal to
private void testComparator() { System.out.println("\n\n testComparator()>>>"); OrderBean order1 = new OrderBean(1,"2018-01-01","beef",10,300); OrderBean order2 = new OrderBean(5,"2018-01-01","Monster meat",80,400); OrderBean order3 = new OrderBean(2,"2018-02-01","beef",100,300); OrderBean order4 = new OrderBean(9,"2018-03-01","Tang Monk meat",2,600); List<OrderBean> list = new ArrayList<OrderBean>(); list.add(order1); list.add(order2); list.add(order3); list.add(order4); /** * ----------------Permutation----------------- * Greater than, less than, equal to * */ //id descending Collections.sort(list, new Comparator<OrderBean>() { public int compare(OrderBean o1, OrderBean o2) { return o2.getId() - o1.getId(); } }); System.out.println("According to the order id Descending order:"+list); //Ascending order of unit price Collections.sort(list, new Comparator<OrderBean>() { public int compare(OrderBean o1, OrderBean o2) { return (int)(o1.getPrice()/o1.getWeight() - o2.getPrice()/o2.getWeight()); } }); System.out.println("In ascending order of order unit price:"+list); System.out.println("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"); /** * ----------------Grouping----------------- * Equal to, not equal to * */ List<List<OrderBean>> byDate = divider(list,new Comparator<OrderBean>() { public int compare(OrderBean o1, OrderBean o2) { return o1.getCdate().equals(o2.getCdate()) ? 0:1; } }); for(int i=0;i<byDate.size();i++) { System.out.println("According to the order cdate Grouping ["+i+"]: "+byDate.get(i)); } }
testComparator()>>> In descending order of order id:[ Order_9: [cdate=2018-03-01, product = Tang Seng meat, weight=2 KG, price=600 RMB,], Order_5: [cdate=2018-01-01, product = monster meat, weight=80 KG, price=400 RMB,], Order_2: [cdate=2018-02-01, product = beef, weight=100 KG, price=300 RMB,], Order_1: [cdate=2018-01-01, product = beef, weight=10 KG, price=300 RMB,]] In ascending order of order unit price:[ Order_2: [cdate=2018-02-01, product = beef, weight=100 KG, price=300 RMB,], Order_5: [cdate=2018-01-01, product = monster meat, weight=80 KG, price=400 RMB,], Order_1: [cdate=2018-01-01, product = beef, weight=10 KG, price=300 RMB,], Order_9: [cdate=2018-03-01, product = Tang Seng meat, weight=2 KG, price=600 RMB,]] \\\\\\\\\\\\\\\\\\ Group by order cdate [0]:[ Order_2: [cdate=2018-02-01, product = beef, weight=100 KG, price=300 RMB,]] Group by order cdate [1]:[ Order_5: [cdate=2018-01-01, product = monster meat, weight=80 KG, price=400 RMB,], Order_1: [cdate=2018-01-01, product = beef, weight=10 KG, price=300 RMB,]] Group by order cdate [2]:[ Order_9: [cdate=2018-03-01, product = Tang Seng meat, weight=2 KG, price=600 RMB,]]
Group tool method
/** * @author wujn * @Description:Group by condition * @param datas * @param c * The criteria of whether it is the same group 0 is the same group, 1 is different group * @return */ public static <T> List<List<T>> divider(Collection<T> datas, Comparator<? super T> c) { List<List<T>> result = new ArrayList<List<T>>(); 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) { // Establish List<T> innerList = new ArrayList<T>(); result.add(innerList); innerList.add(t); } } return result; }