Java8 - 使用 Comparator.comparing 進行比較排序java
當咱們須要對集合的元素進行排序的時候,能夠使用java.util.Comparator 建立一個比較器來進行排序。Comparator接口一樣也是一個函數式接口,咱們能夠把使用lambda表達式。以下示例,app
package com.common; import java.util.*; import java.util.stream.Collectors; public class ComparatorTest { public static void main(String[] args) { Employee e1 = new Employee("John", 25, 3000, 9922001); Employee e2 = new Employee("Ace", 22, 2000, 5924001); Employee e3 = new Employee("Keith", 35, 4000, 3924401); List<Employee> employees = new ArrayList<>(); employees.add(e1); employees.add(e2); employees.add(e3); /** * @SuppressWarnings({"unchecked", "rawtypes"}) * default void sort(Comparator<? super E> c) { * Object[] a = this.toArray(); * Arrays.sort(a, (Comparator) c); * ListIterator<E> i = this.listIterator(); * for (Object e : a) { * i.next(); * i.set((E) e); * } * } * * sort 對象接收一個 Comparator 函數式接口,能夠傳入一個lambda表達式 */ employees.sort((o1, o2) -> o1.getName().compareTo(o2.getName())); Collections.sort(employees, (o1, o2) -> o1.getName().compareTo(o2.getName())); employees.forEach(System.out::println); } } /** * [Employee(name=John, age=25, salary=3000.0, mobile=9922001), * Employee(name=Ace, age=22, salary=2000.0, mobile=5924001), * Employee(name=Keith, age=35, salary=4000.0, mobile=3924401)] */ class Employee { String name; int age; double salary; long mobile; // constructors, getters & setters public Employee(String name, int age, double salary, long mobile) { this.name = name; this.age = age; this.salary = salary; this.mobile = mobile; } 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; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } public long getMobile() { return mobile; } public void setMobile(long mobile) { this.mobile = mobile; } @Override public String toString() { final StringBuilder sb = new StringBuilder("Employee{"); sb.append("name='").append(name).append('\''); sb.append(", age=").append(age); sb.append(", salary=").append(salary); sb.append(", mobile=").append(mobile); sb.append('}'); return sb.toString(); } }
查看 Comparator 類內部實現,還有一個 comparing 方法,實現以下,ide
public static <T, U extends Comparable<? super U>> Comparator<T> comparing( Function<? super T, ? extends U> keyExtractor) { Objects.requireNonNull(keyExtractor); return (Comparator<T> & Serializable) (c1, c2) -> keyExtractor.apply(c1).compareTo(keyExtractor.apply(c2)); }
其返回值是 (c1, c2) -> keyExtractor.apply(c1).compareTo(keyExtractor.apply(c2)); 一個lambda表達式,也就是一個Compator 。因此上面那個例子也能夠改形成 以下,函數
package com.common; import java.util.*; public class ComparatorTest { public static void main(String[] args) { Employee e1 = new Employee("John", 25, 3000, 9922001); Employee e2 = new Employee("Ace", 22, 2000, 5924001); Employee e3 = new Employee("Keith", 35, 4000, 3924401); List<Employee> employees = new ArrayList<>(); employees.add(e1); employees.add(e2); employees.add(e3); /** * @SuppressWarnings({"unchecked", "rawtypes"}) * default void sort(Comparator<? super E> c) { * Object[] a = this.toArray(); * Arrays.sort(a, (Comparator) c); * ListIterator<E> i = this.listIterator(); * for (Object e : a) { * i.next(); * i.set((E) e); * } * } * * sort 對象接收一個 Comparator 函數式接口,能夠傳入一個lambda表達式 */ employees.sort((o1, o2) -> o1.getName().compareTo(o2.getName())); Collections.sort(employees, (o1, o2) -> o1.getName().compareTo(o2.getName())); employees.forEach(System.out::println); /** * Comparator.comparing 方法的使用 * * comparing 方法接收一個 Function 函數式接口 ,經過一個 lambda 表達式傳入 * */ employees.sort(Comparator.comparing(e -> e.getName())); /** * 該方法引用 Employee::getName 能夠代替 lambda表達式 */ employees.sort(Comparator.comparing(Employee::getName)); } } /** * [Employee(name=John, age=25, salary=3000.0, mobile=9922001), * Employee(name=Ace, age=22, salary=2000.0, mobile=5924001), * Employee(name=Keith, age=35, salary=4000.0, mobile=3924401)] */ class Employee { String name; int age; double salary; long mobile; // constructors, getters & setters public Employee(String name, int age, double salary, long mobile) { this.name = name; this.age = age; this.salary = salary; this.mobile = mobile; } 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; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } public long getMobile() { return mobile; } public void setMobile(long mobile) { this.mobile = mobile; } @Override public String toString() { final StringBuilder sb = new StringBuilder("Employee{"); sb.append("name='").append(name).append('\''); sb.append(", age=").append(age); sb.append(", salary=").append(salary); sb.append(", mobile=").append(mobile); sb.append('}'); return sb.toString(); } }
public static <T, U> Comparator<T> comparing( Function<? super T, ? extends U> keyExtractor, Comparator<? super U> keyComparator) { Objects.requireNonNull(keyExtractor); Objects.requireNonNull(keyComparator); return (Comparator<T> & Serializable) (c1, c2) -> keyComparator.compare(keyExtractor.apply(c1), keyExtractor.apply(c2)); }
和comparing 方法一不一樣的是 該方法多了一個參數 keyComparator ,keyComparator 是建立一個自定義的比較器。ui
Collections.sort(employees, Comparator.comparing( Employee::getName, (s1, s2) -> { return s2.compareTo(s1); }));
返回相反的排序規則,this
/** * 相反的排序規則 */ Collections.sort(employees, Comparator.comparing(Employee::getName).reversed()); employees.forEach(System.out::println);
輸出,spa
Employee{name='Keith', age=35, salary=4000.0, mobile=3924401} Employee{name='John', age=25, salary=3000.0, mobile=9922001} Employee{name='Ace', age=22, salary=2000.0, mobile=5924001}
當集合中存在null元素時,能夠使用針對null友好的比較器,null元素排在集合的最前面code
employees.add(null); //插入一個null元素 Collections.sort(employees, Comparator.nullsFirst(Comparator.comparing(Employee::getName))); employees.forEach(System.out::println); Collections.sort(employees, Comparator.nullsLast(Comparator.comparing(Employee::getName))); employees.forEach(System.out::println);
首先使用 name 排序,緊接着在使用ege 排序,來看下使用效果對象
Collections.sort(employees, Comparator.comparing(Employee::getAge).thenComparing(Employee::getName)); employees.forEach(System.out::println);
=======END=======排序