Java 我的比较器';s比较法是';行不通
我有一个Customer对象类,它有一些变量,并且已经实现了一个关于其中一个变量的比较器。但是,我需要为不同的变量last_name实现另一个比较器 因为我的Customer类中不能有2个compareTo()方法,所以我决定专门为此创建一个比较类Java 我的比较器';s比较法是';行不通,java,sorting,collections,comparator,Java,Sorting,Collections,Comparator,我有一个Customer对象类,它有一些变量,并且已经实现了一个关于其中一个变量的比较器。但是,我需要为不同的变量last_name实现另一个比较器 因为我的Customer类中不能有2个compareTo()方法,所以我决定专门为此创建一个比较类 public class CompareByLastName implements Comparator<Customer> { private List<Purchase> purchases; priva
public class CompareByLastName implements Comparator<Customer> {
private List<Purchase> purchases;
private List<Customer> customers;
public CompareByLastName(List<Purchase> purchases, List<Customer> customers) {
this.purchases = purchases;
this.customers = customers;
}
/**
* @param descending
* @return will be a sorted, in ascending, or descending, array of customer's according to their authors.
*/
public List<Purchase> sortByLastName(boolean descending){
List<Purchase> return_List = new LinkedList<Purchase>();
Collections.sort(customers);
if(descending == true) {
Collections.reverse(customers);
}
for(Customer customer : customers) {
for(Purchase purchase_info : purchases) {
if(customer.getId() == purchase_info.getCustomer_id()) {
return_List.add(purchase_info);
}
}
}
return return_List;
}
@Override
public int compare(Customer customer_1, Customer customer_2) {
int result = customer_1.getLastName().compareTo(customer_2.getLastName());
if(result < 0) {
return -1;
}
else if(result > 0) {
return 1;
}
else {
return 0;
}
}
}
公共类CompareByLastName实现Comparator{
私人清单购买;
私人名单客户;
公共CompareByLastName(列出采购、列出客户){
这是购买=购买;
这是。顾客=顾客;
}
/**
*@param递减
*@return将是按作者排序的、升序或降序的客户数组。
*/
公共列表sortByLastName(布尔值递减){
List return_List=newlinkedlist();
收集、分类(客户);
如果(降序==真){
收款。反向(客户);
}
用于(客户:客户){
用于(采购信息:采购){
if(customer.getId()==purchase\u info.getCustomer\u id()){
退货清单。添加(采购信息);
}
}
}
返回列表;
}
@凌驾
公共整数比较(客户1、客户2){
int result=customer_1.getLastName().compareTo(customer_2.getLastName());
如果(结果<0){
返回-1;
}
否则,如果(结果>0){
返回1;
}
否则{
返回0;
}
}
}
但一旦它进入集合。排序(客户)
它不会激活下面的公共int比较(Customer-Customer\u 1,Customer-Customer\u 2)
坦率地说,我不知道它作为一种比较的使用;有人知道如何解决这个问题并按姓氏排序吗
哦,一旦它得到了退货,如何从100(0-99)件采购物品变为103(0-102)件退货清单?不知道这是怎么发生的
修复了这一部分,我切换了循环,读取购买,然后查看所有客户的列表,找到匹配项,而不是反之亦然
感谢您的帮助
提前感谢。您没有使用
比较方法
Collections.sort(customers);
上面这一行按照客户的自然顺序(由Customer.compareTo()
定义)对客户进行排序,而不是根据您的比较器。相反,您可以这样做:
Collections.sort(customers, this);
现在您的CompareByLastName
将用作排序中的比较器
两个旁白:
- 您在comparator类中使用sort方法的设计是非常规的。不过,这应该行得通
- 在
compare
方法中,不需要if-else构造。以下简单得多的实现就足够了:
return customer_1.getLastName().compareTo(customer_2.getLastName());
您的比较器的compare
方法不起作用,因为您没有将比较器的实例传递给集合。sort
。谢谢:D我在.sort()中尝试了许多第二个参数。我从来没有想到this关键字会起作用。@Mike_1234原因,为什么解决方案对您来说不明显,你的整个设计都是反直觉的。您不应将比较器
实现与执行排序操作的代码混合使用。事实上,您不需要自己实现比较器。只是Collections.sort(customers,Comparator.comparing(Customer::getLastName))代码>将起作用。甚至更简单,customers.sort(Comparator.comparing(Customer::getLastName))代码>。但事实上,那不是你想要的。您的目标是对购买列表进行排序,因此您应该这样做&避免执行n×m的嵌套循环operations@Mike_1234例如,public static List sortPurchaseByCustomersLastName(列出客户,列出购买,布尔降序){List returnList=new ArrayList(购买);Map idToName=customers.stream().collect(Collectors.toMap(Customer::getId,Customer::getLastName));Comparator c=Comparator.comparing(p->idToName.get(p.getCustomer_id());if(降序){c=c.reversed();}returnList.sort(c);returnList;}
。建议阅读: