一个人如果没有梦想,跟无忧无虑有什么区别呢?
今天来给大家介绍下java.utils包下的Comparator接口。该接口代表一个比较器,java数组工具类和集合工具类中提供对sort方法排序就是使用 Comparator接口来处理排序的。
Comparator接口中有一个方法int compare(T o1, T o2)。这个方法返回值是int类型,如果返回值小于0,说明比较结果是o1<o2,如果返回值等于0,说明比较结果是o1=o2,如果返回值大于0,则说明比较结果是o1>o2。
既然是接口,那么我们就可以实现它,来自定义其中对比较规则,即可实现在一个List列表中将元素按照某个属性进行排序。
下面我们用代码举例说明,如下:
package com.java.utils.Comparator; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; /** * 类说明: * 比较器Comparator接口的应用 */ public class ComparatorTest { /** * 实体对象类 */ static class User { String name; int age; public User(String name,int age) { this.name=name; this.age=age; } 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; } } /** * 自定义比较器 * 根据名称比较 */ static class MyComparatorName implements Comparator<User>{ @Override public int compare(User u1, User u2) { return u1.getName().compareTo(u2.getName()) * -1; } } /** * 自定义比较器 * 根据年龄比较 */ static class MyComparatorAge implements Comparator<User>{ @Override public int compare(User u1, User u2) { return (u1.getAge() - u2.getAge()); } } //测试 public static void main(String[] args) { List<User> list =new ArrayList<User>(); User u1 = new User("a", 2); User u2 = new User("d", 0); User u3 = new User("c", 3); list.add(u1); list.add(u2); list.add(u3); Collections.sort(list, new MyComparatorName()); for(User u :list){ System.out.println(u.getName()); } Collections.sort(list, new MyComparatorAge()); for(User u :list){ System.out.println(u.getAge()); } } }
在上面对例子中,我们首先按照名称从大到小进行排序,输出结果是d、c、a然后又按照年龄从小到大进行排序,输出结果是0、2、3。
需要注意的是,Collections.sort方法默认是按照返回结果从小到大进行排序,如果我们需要从大到小则在实现比较器大时候将返回结果乘以-1即可。
是不是很简单?如果是我们自己实现比较器,那么我们就可以根据实际的业务需求去实现排序。
例题:
import java.lang.ref.WeakReference; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Set; import java.util.*; import java.util.stream.Collectors; public class test { public static void main(String[] args) { List<Employee> list=new ArrayList<>(); list.add(new Employee(1,10,5500.00)); list.add(new Employee(2,20,4800.00)); list.add(new Employee(3,40,14500.00)); list.add(new Employee(4,40,44500.00)); list.add(new Employee(5,50,6500.00)); list.add(new Employee(6,50,7500.00)); list.add(new Employee(7,10,4500.00)); list.add(new Employee(8,40,6500.00)); list.add(new Employee(9,20,1900.00)); Comparator<Employee> groupComparator = new Comparator<Employee>() { @Override public int compare(Employee o1, Employee o2) { return o1.getDeptid()-o2.getDeptid(); } }; Collections.sort(list,groupComparator); //先根据deptid分组 Comparator<Employee> salaryComparator=new Comparator<Employee>() { @Override public int compare(Employee o1, Employee o2) { return o1.getDeptid()==o2.getDeptid()?(int) (o2.getSalary() - o1.getSalary()):0 ; } }; Collections.sort(list,salaryComparator); //然后根据工资降序 //设置等级rank Employee employee = list.get(0); int deptid = employee.getDeptid(); int k=1; employee.setRank(k); for (int i = 1; i < list.size(); i++) { Employee employee1 = list.get(i); int deptid1 = employee1.getDeptid(); if(deptid==deptid1){ employee1.setRank(++k); }else{ k=1; employee1.setRank(k); deptid=employee1.getDeptid(); } } //打印Employee for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } } } class Employee{ private int empid; public int getEmpid() { return empid; } public void setEmpid(int empid) { this.empid = empid; } public int getDeptid() { return deptid; } public void setDeptid(int deptid) { this.deptid = deptid; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } private int deptid; private double salary; private int rank; public Employee(int empid, int deptid, double salary) { this.empid = empid; this.deptid = deptid; this.salary = salary; } @Override public String toString() { return "Employee{" + "empid=" + empid + ", deptid=" + deptid + ", salary=" + salary + ", rank=" + rank + '}'; } public void setRank(int rank) { this.rank = rank; } }