比较器基于对象的不同可空字段 [英] Comparator based on different nullable fields of an object

查看:168
本文介绍了比较器基于对象的不同可空字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Employee 对象,其中包含两个字段 name jobTitle 。对于员工对象的排序,第一优先级应该是 jobTitle ,如果 jobTitle 为null,那么排序应该基于名称。

I have an Employee object which contains two fields name and jobTitle. For sorting the employee objects, first priority should be on jobTitle, if jobTitle is null then the sorting should be based on name.

以下是员工对象

public class Employee {
    private String name;
    private String jobTitle;
}

我使用链式比较器与 JobTitlecomparator NameComparator 实现此目的:

I used chained Comparator with JobTitlecomparator and NameComparator to achieve this:

public class EmployeeChainedComparator implements Comparator<Employee> {

    private List<Comparator<Employee>> listComparators;

    @SafeVarargs
    public EmployeeChainedComparator(Comparator<Employee>... comparators) {
        this.listComparators = Arrays.asList(comparators);
    }

    @Override
    public int compare(Employee emp1, Employee emp2) {
        for (Comparator<Employee> comparator : listComparators) {
            int result = comparator.compare(emp1, emp2);
            if (result != 0) {
                return result;
            }
        }
        return 0;
    }
}

public class EmployeeJobTitleComparator implements Comparator<Employee> {

    @Override
    public int compare(Employee emp1, Employee emp2) {
        if(emp1.getJobTitle() != null && emp2.getJobTitle() != null){
            return emp1.getJobTitle().compareTo(emp2.getJobTitle());
        } else {
            return 0;
        }
    }
}

public class EmployeeNameComparator implements Comparator<Employee> {

    @Override
    public int compare(Employee emp1, Employee emp2) {
        return emp1.getName().compareTo(emp2.getName());
    }
}

public class SortingMultipleAttributesExample {
    public static void main(String[] args) {
        List<Employee> listEmployees = new ArrayList<Employee>();
        listEmployees.add(new Employee("Tom", "Developer"));
        listEmployees.add(new Employee("Sam", null));
        listEmployees.add(new Employee("Tim", "Designer"));
        listEmployees.add(new Employee("Bob", null));
        listEmployees.add(new Employee("Peter", null));
        listEmployees.add(new Employee("Craig", "Programmer"));

        Collections.sort(listEmployees, new EmployeeChainedComparator(new EmployeeJobTitleComparator(), new EmployeeNameComparator()
                ));

        for(Employee emp : listEmployees){
            System.out.println("Employee Job: "+emp.getJobTitle()+" Employee Name: "+emp.getName());
        }
    }
}

现在我应该得到输出喜欢这个

Now I should get the output like this

Employee Job: Designer Employee Name: Tim
Employee Job: Developer Employee Name: Tom
Employee Job: Programmer Employee Name: Craig
Employee Job: null Employee Name: Bob
Employee Job: null Employee Name: Peter
Employee Job null Employee Name: Sam

但是我没有达到预期的结果。我得到这样的输出

But I'm not getting the desired result as I expected. I'm getting the output like this

Employee Job Developer Employee Name Tom
Employee Job null Employee Name Sam
Employee Job Designer Employee Name Tim
Employee Job null Employee Name Bob
Employee Job null Employee Name Peter
Employee Job Programmer Employee Name Craig

任何人都可以帮我解决这个问题吗?

Can anyone help me on how to achieve this?

推荐答案

如果 的标题是 null ,那么两个 Employee s将评估为等于,即使其中一个不为空。那不是你想要的。您希望所有 null 标题彼此相等,但不是非空值。

If either of the titles is null, then the two Employees will evaluate as equals, even if one of them is not null. That's not what you want. You want all null titles to be equal to each other, but not non-null values.

替换您的比较使用此方法:

Replace your compare method with this:

public int compare(Employee emp1, Employee emp2) {
    if(emp1.getJobTitle() == null && emp2.getJobTitle() == null){
        return 0;
    }
    if(emp1.getJobTitle() == null) return 1;
    if(emp2.getJobTitle() == null) return -1;
    return emp1.getJobTitle().compareTo(emp2.getJobTitle());
}

你应该得到你期望的结果。

And you should get the results you expect.

这篇关于比较器基于对象的不同可空字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆