ArrayList sort() 方法根据指定的 Comparator 实例引入的顺序对列表进行排序。
列表中的所有元素必须可以相互比较。
之路教程 https://onitr oad .com
ArrayList sort() - 按字段对对象列表进行排序
当我们想要对给定的对象列表排序但不是按自然顺序排序时,比较器最有用。
例如,员工列表自然应按其员工 ID 进行排序。
比较器的另一个用例是当列表中的对象没有实现 Comparable 接口时。
此外,我们可能希望在不同场景下以不同方式对对象列表进行排序。
例如,员工列表可以按姓名、年龄或者任何其他此类字段进行排序。
比较器最适合这些类型的要求。
Employee类型
让我们创建一个只有 3 个字段的简单Employee类。
import java.time.LocalDate; public class Employee { private Long id; private String name; private LocalDate dob; public Employee(Long id, String name, LocalDate dob) { super(); this.id = id; this.name = name; this.dob = dob; } //Getters and Setters @Override public String toString() { return "Employee [id=" + id + ", name=" + name + ", dob=" + dob + "]"; } }
比较器类以启用对不同字段的排序
比较器按姓名字母顺序对员工列表进行排序。
import java.util.Comparator; public class NameSorter implements Comparator<Employee> { @Override public int compare(Employee o1, Employee o2) { return o2.getName().compareToIgnoreCase(o1.getName()); } }
比较器按年龄升序对员工列表进行排序。
import java.util.Comparator; public class AgeSorter implements Comparator<Employee> { @Override public int compare(Employee o1, Employee o2) { return o2.getDob().compareTo(o1.getDob()); } }
比较器按 id 对员工列表进行排序。
import java.util.Comparator; public class IdSorter implements Comparator<Employee> { @Override public int compare(Employee o1, Employee o2) { return o2.getId().compareTo(o1.getId()); } }
ArrayList sort()方法 示例
现在让我们使用比较器类按姓名对员工列表进行排序。
import java.time.LocalDate; import java.time.Month; import java.util.ArrayList; public class ArrayListExample { public static void main(String[] args) throws CloneNotSupportedException { ArrayList<Employee> employees = new ArrayList<>(); employees.add(new Employee(1l, "JackLi", LocalDate.of(2018, Month.APRIL, 21))); employees.add(new Employee(4l, "BobRobert", LocalDate.of(2018, Month.APRIL, 22))); employees.add(new Employee(3l, "Tomm", LocalDate.of(2018, Month.APRIL, 25))); employees.add(new Employee(5l, "Lucie", LocalDate.of(2018, Month.APRIL, 23))); employees.add(new Employee(2l, "Edwin", LocalDate.of(2018, Month.APRIL, 24))); employees.sort(new NameSorter()); System.out.println(employees); } }
程序输出。
[ Employee [id=1, name=JackLi, dob=2018-04-21], Employee [id=4, name=BobRobert, dob=2018-04-22], Employee [id=3, name=Tomm, dob=2018-04-25], Employee [id=5, name=Lucie, dob=2018-04-23], Employee [id=2, name=Edwin, dob=2018-04-24]]
现在让我们使用比较器类对 age 列表进行排序。
import java.time.LocalDate; import java.time.Month; import java.util.ArrayList; public class ArrayListExample { public static void main(String[] args) throws CloneNotSupportedException { ArrayList<Employee> employees = new ArrayList<>(); employees.add(new Employee(1l, "JackLi", LocalDate.of(2018, Month.APRIL, 21))); employees.add(new Employee(4l, "BobRobert", LocalDate.of(2018, Month.APRIL, 22))); employees.add(new Employee(3l, "Tomm", LocalDate.of(2018, Month.APRIL, 25))); employees.add(new Employee(5l, "Lucie", LocalDate.of(2018, Month.APRIL, 23))); employees.add(new Employee(2l, "Edwin", LocalDate.of(2018, Month.APRIL, 24))); employees.sort(new AgeSorter()); System.out.println(employees); } }
程序输出。
[ Employee [id=1, name=JackLi, dob=2018-04-21], Employee [id=4, name=BobRobert, dob=2018-04-22], Employee [id=5, name=Lucie, dob=2018-04-23], Employee [id=2, name=Edwin, dob=2018-04-24], Employee [id=3, name=Tomm, dob=2018-04-25]]
ArrayList sort() 方法
sort() 方法接受一个 Comparator
实现类的实例,它必须能够比较包含在数组列表中的元素。
在内部,sort()
方法使用Arrays.sort()
方法对列表中的元素进行比较和排序。
public void sort(Comparator<? super E> c) { final int expectedModCount = modCount; Arrays.sort((E[]) elementData, 0, size, c); if (modCount != expectedModCount) { throw new ConcurrentModificationException(); } modCount++; }
方法参数 Comparator 实现。
方法返回 void
。
如果列表被修改而 sort()
方法未完成,则该方法抛出 ConcurrentModificationException
。
日期:2020-09-17 00:09:57 来源:oir作者:oir