简体   繁体   English

在java中使用单个Comparator接口对具有多个字段的员工对象进行排序

[英]Using single Comparator interface to sort the employee object with multiple fields in java

Consider scenario below :考虑以下场景:

Suppose there is Employee Class having attributes : eID, eName, eAge, eAddress, eDeptName and can be added more in future based on requirement.假设有 Employee 类具有以下属性:eID、eName、eAge、eAddress、eDeptName,将来可以根据需要添加更多。

This Employee object is added in to hashmap as key and string as value.这个 Employee 对象被添加到 hashmap 作为键和字符串作为值。

Consider the below code in main class :考虑主类中的以下代码:

     class Test
    {
        public static void main(String[] args)
       {
          Map<Employee,String> m = new HashMap<>(); 
          //Adding object in to map
          m.put(e1,"one"); 
          m.put(e2,"two"); 
          m.put(e3,"othree");
        }
    }

Now I need to use the comparator interface to sort the map based on employee object.现在我需要使用比较器接口根据员工对象对地图进行排序。

My Questions is :-我的问题是:-

1) what is the best common used way to sort the employee object : by using seperate comparator for each field or using the single comparator for all fields 1)对员工对象进行排序的最常用方法是什么:对每个字段使用单独的比较器或对所有字段使用单个比较器

2) How to use the single comparator for all fields and how to invoke it ? 2)如何对所有字段使用单个比较器以及如何调用它?

Thanks in Advance提前致谢

1) what is the best common used way to sort the employee object : by using seperate comparator for each field or using the single comparator for all fields 1)对员工对象进行排序的最常用方法是什么:对每个字段使用单独的比较器或对所有字段使用单个比较器

There is no best way, but only what you need.没有最好的方法,只有你需要的。 You can have separate comparator for each field so that you can sort your list by specific field.您可以为每个字段使用单独的比较器,以便您可以按特定字段对列表进行排序。 You will have more choices over how your list is to be sorted.您将有更多关于如何对列表进行排序的选择。

Or you can use multiple fields in the same comparator when you need them as tie-breaker(s).或者,当您需要它们作为决胜局时,您可以在同一个比较器中使用多个字段。 But you have to decide which is the primary sorting criteria, which are the secondary criteria(s).但是您必须决定哪些是主要分类标准,哪些是次要标准。

When making a decision, ask yourself:做决定时,问问自己:

  • about the reusability of the design.关于设计的可重用性 Is grouping them as 1 comparator more useful than separating them?将它们分组为 1 个比较器比将它们分开更有用吗?
  • what is the likelihood of using it again if you were to group them as 1 comparator?如果您将它们分组为 1 个比较器,再次使用它的可能性有多大?
  • which one is more flexible to be used?哪一种使用起来更灵活?
  • which one is easier to maintain?哪个更容易维护?

2) How to use the single comparator for all fields and how to invoke it ? 2)如何对所有字段使用单个比较器以及如何调用它?

If you want to use all the fields, say 4 fields: name , age , salary , gender (using my own fields as example)如果要使用所有字段,请说 4 个字段: nameagesalarygender (以我自己的字段为例)

You choose only 1 field which you are interest to use as the sorting criteria.您只选择 1 个您有兴趣用作排序标准的字段。 The other 3 fields will be used as tie-breakers.其他 3 个字段将用作决胜局。

Assuming you sort by name .假设您按name排序。 When 2 similar names appear, you can use a 2nd field as the tie-breaker such as age .当出现 2 个相似的名字时,您可以使用第二个字段作为决胜局,例如age

If there are multiple employees with same name and age, you sort them by the 3rd criteria, maybe by salary .如果有多位员工的姓名和年龄相同,您可以按照第三个标准对他们进行排序,也许是按照salary If all 3 fields are similar, then you sort them by the 4th criteria gender .如果所有 3 个字段都相似,则按第 4 个标准对它们进行排序gender

//example:
class EmployeeComparator implements Comparator<Employee>{
    @Override
    public int compare(Employee e1, Employee e2){
        int cmp = e1.getName().compareTo(e2.getName());
        if(cmp == 0)
            cmp = e1.getAge() - e2.getAge();
        if(cmp == 0)
            cmp = e1.getSalary() - e2.getSalary();
        if(cmp == 0)
            cmp = e1.getGender().compareTo(e2.getGender());
        return cmp;
    }   
}

The implementation is up to you.实施取决于您。 However, you'll have to determine how to define how to compare an employee to another.但是,您必须确定如何定义将员工与其他员工进行比较的方式。 For example are they compared by ID?例如,它们是否通过 ID 进行比较? Name?姓名? Age?年龄?

You can use a combination of all of the fields in the compare method, a few of them that make sense or just one of them.您可以使用 compare 方法中所有字段的组合,其中一些有意义或仅使用其中之一。

Since Comparator is an interface, you'll have to override the compare and equals method and define the logic in which attributes you'll compare in there.由于 Comparator 是一个接口,您必须覆盖 compare 和 equals 方法并定义您将在其中比较哪些属性的逻辑。

Let Employee implement Comparable interface, and include all fields you want in compareTo() method.让 Employee 实现 Comparable 接口,并在 compareTo() 方法中包含您想要的所有字段。 Then, if you just use TreeMap instead of HashMap, your map items will be automatically sorted.然后,如果您只使用 TreeMap 而不是 HashMap,您的地图项将自动排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM