java中的比较器
时间:2020-02-23 14:34:45 来源:igfitidea点击:
在本教程中,我们将看到如何使用比较器在Java中排序对象列表。
比较器(Comparator):
如果要对类的对象列表进行排序,可以使用比较器接口。
我们不需要在类中执行比较器,其对象需要对其进行排序。
我们可以创建单独的类并实现如下的比较器接口。
例如:
package com.arpit.theitroad; import java.util.Comparator; public class EmployeeSortByIdComparator implements Comparator{ @Override public int compare(Employee e1, Employee e2) { return e1.getEmpId()-e2.getEmpId(); } }
我们可以根据需要排序的不同属性使用不同的排序逻辑。
例如:假设我们想要按名称对员工列表进行排序,我们可以在OCDE下面使用。
Employee e1= new Employee(4, "John", 20); Employee e2= new Employee(3, "Martin", 40); Employee e3= new Employee(1, "Mary", 28); Employee e4= new Employee(2, "Andrew", 35); List<Employee> listofEmployees=new ArrayList<>(); listofEmployees.add(e1); listofEmployees.add(e2); listofEmployees.add(e3); listofEmployees.add(e4); System.out.println("Before Sorting by name: "); for (Employee e:listofEmployees) { System.out.println("Employee Id: "+e.getEmpId()+"|| name: "+e.getName()); } Collections.sort(listofEmployees,new Comparator<Employee>() { @Override public int compare(Employee o1, Employee o2) { return o1.getName().compareTo(o2.getName()); } }); System.out.println("After Sorting by name: "); for (Employee e:listofEmployees) { System.out.println("Employee Id: "+e.getEmpId()+"|| name: "+e.getName()); }
比较器Java代码:
创建一个名为emploomee.java的类,该类将有empid,名称和年龄。
1.Employee.java.
package com.arpit.theitroad; public class Employee { private int empId; private String name; private int age; public Employee(int empId, String name, int age) { super(); this.empId = empId; this.name = name; this.age = age; } public int getEmpId() { return empId; } public void setEmpId(int empId) { this.empId = empId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
创建一个名为"employeoherortbyIdcomparator"的类。
此类将通过Empid对员工进行排序列表。
2.employeAortByIdComparator.java
package com.arpit.theitroad; import java.util.Comparator; public class EmployeeSortByIdComparator implements Comparator{ @Override public int compare(Employee e1, Employee e2) { return e1.getEmpId()-e2.getEmpId(); } }
让我们创建主类,该类将有创建对象列表并根据Empid排序它。
- employeecomparatormain.java.
package com.arpit.theitroad; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class EmployeeComparatorMain { public static void main(String[] args) { Employee e1= new Employee(4, "John", 20); Employee e2= new Employee(3, "Martin", 40); Employee e3= new Employee(1, "Mary", 28); Employee e4= new Employee(2, "Andrew", 35); List<Employee> listofEmployees=new ArrayList<>>(); listofEmployees.add(e1); listofEmployees.add(e2); listofEmployees.add(e3); listofEmployees.add(e4); System.out.println("Before Sorting by empId: "); for (Employee e:listofEmployees) { System.out.println("Employee Id: "+e.getEmpId()+"|| name: "+e.getName()); } Collections.sort(listofEmployees,new EmployeeSortByIdComparator()); System.out.println("After Sorting by empId: "); for (Employee e:listofEmployees) { System.out.println("Employee Id: "+e.getEmpId()+"|| name: "+e.getName()); } } }
输出:
Before Sorting by empId: Employee Id: 4|| name: John Employee Id: 3|| name: Martin Employee Id: 1|| name: Mary Employee Id: 2|| name: Andrew After Sorting by empId: Employee Id: 1|| name: Mary Employee Id: 2|| name: Andrew Employee Id: 3|| name: Martin Employee Id: 4|| name: John
匿名比较器:
例如,比较器上的比较器的一个优势是创建匿名比较器:我们不需要为需要对其进行排序的对象来实现对类的类相对的接口。
让我们在榜样的帮助下了解更多信息:我们将使用匿名类按名称对员工列表进行排序。
ComparatorMain.java.
package com.arpit.theitroad; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class EmployeeComparatorMain { public static void main(String[] args) { Employee e1= new Employee(4, "John", 20); Employee e2= new Employee(3, "Martin", 40); Employee e3= new Employee(1, "Mary", 28); Employee e4= new Employee(2, "Andrew", 35); List<Employee> listofEmployees=new ArrayList<>>(); listofEmployees.add(e1); listofEmployees.add(e2); listofEmployees.add(e3); listofEmployees.add(e4); System.out.println("Before Sorting by name: "); for (Employee e:listofEmployees) { System.out.println("Employee Id: "+e.getEmpId()+"|| name: "+e.getName()); } Collections.sort(listofEmployees,new Comparator<Employee>() { @Override public int compare(Employee o1, Employee o2) { return o1.getName().compareTo(o2.getName()); } }); System.out.println("After Sorting by name: "); for (Employee e:listofEmployees) { System.out.println("Employee Id: "+e.getEmpId()+"|| name: "+e.getName()); } } }
输出:
Before Sorting by name: Employee Id: 4|| name: John Employee Id: 3|| name: Martin Employee Id: 1|| name: Mary Employee Id: 2|| name: Andrew After Sorting by name: Employee Id: 2|| name: Andrew Employee Id: 4|| name: John Employee Id: 3|| name: Martin Employee Id: 1|| name: Mary
正如我们在这里看到的,我们使用匿名比较器按名称对员工列表进行排序。
我们在此没有创建比较器特定的程序。