一、簡介
<T> Comparator<T>
是一個函數接口,用於比較兩個對象。使用它可根據需要確定按升序或降序排列。在Java API中,這個接口是一個功能強大的方式,可以用於排序、搜索、treeSet和PriorityQueue。而為了更好的排序功能,java新的comparator中新增了一個新的允許使用Lambda表達式來代替普通方法的排序規則接口:newcomparator
二、newcomparator與comparator的區別
newcomparator可以用來定義基於lambda表達式的排序規則。而對於原有的comparator,則必須用定義一個類並實現comparable接口,這樣在類中便可以對需要排序的元素進行自定義的排序規則。
三、newcomparator的使用方法
在使用newcomparator時,需要根據排序規則建立lambda表達式並作為newcomparator的參數。例如下面這個例子演示怎樣使用newcomparator來對一個字符串列表進行不區分大小寫的排序。
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("apple");
list.add("banana");
list.add("orange");
list.add("Pear");
list.sort(String.CASE_INSENSITIVE_ORDER);
list.forEach(System.out::println);
}
四、newcomparator和stream的配合使用
Java 8的新特性增加了stream api。stream api不僅擴展了對集合的操作,也很好的與newcomparator配合使用。通過對字段進行過濾、排序和分組,可以快速生成自定義的聚合數據。
public class Student {
private String name;
private int score;
public Student(String name, int score) {
super();
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
public class NewComparatorDemo {
public static void main(String[] args) {
List<Student> list = new ArrayList();
list.add(new Student("Tom", 90));
list.add(new Student("Jerry", 80));
list.add(new Student("Tim", 85));
list.add(new Student("Sunny", 89));
list.add(new Student("Mary", 95));
// 按分數排序
list.stream().sorted(Comparator.comparing(Student::getScore)).forEach(System.out::println);
// 輸出結果:
// Jerry 80
// Tim 85
// Sunny 89
// Tom 90
// Mary 95
// 按名字排序
list.stream().sorted(Comparator.comparing(Student::getName)).forEach(System.out::println);
// 輸出結果:
// Jerry 80
// Mary 95
// Sunny 89
// Tim 85
// Tom 90
}
}
五、數據類型的排序
對於基本數據類型,可以直接使用以下的寫法
int[] nums = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
Arrays.stream(nums).boxed().sorted(Comparator.reverseOrder())
.forEach(System.out::println);
// 輸出結果:
// 9
// 6
// 5
// 5
// 5
// 4
// 3
// 3
// 2
// 1
// 1
而對於對象類型,需要根據字段進行排序。下面的例子演示了怎樣用數組排序來實現對自定義對象集合的排序。
public class Employee {
private String name;
private Integer age;
private Double salary;
// 省略get/set方法
public static void main(String[] args) {
Employee[] employees = new Employee[4];
employees[0] = new Employee("Tom", 23, 5000.0);
employees[1] = new Employee("Jerry", 21, 4500.0);
employees[2] = new Employee("Sunny", 25, 5500.0);
employees[3] = new Employee("Marry", 23, 6500.0);
Arrays.sort(employees, Comparator.comparing(Employee::getAge).reversed());
System.out.println("按年齡降序排序的結果:");
Arrays.stream(employees).forEach(System.out::println);
// 輸出結果:
// Marry 23 6500.0
// Tom 23 5000.0
// Sunny 25 5500.0
// Jerry 21 4500.0
Arrays.sort(employees, Comparator.comparingDouble(Employee::getSalary));
System.out.println("按工資升序排序的結果:");
Arrays.stream(employees).forEach(System.out::println);
// 輸出結果:
// Jerry 21 4500.0
// Tom 23 5000.0
// Sunny 25 5500.0
// Marry 23 6500.0
}
}
原創文章,作者:XWZOP,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/317476.html