【Java 8 新特性】Java Comparator.naturalOrder | 自然排序
https://blog.csdn.net/qq_31635851/article/details/120324130
naturalOrder
是比较器功能接口的静态方法。
Java 8
中引入的Comparator.naturalOrder
方法返回一个比较器,该比较器以自然顺序比较可比较对象。
对于自然排序,一个类需要实现Comparable
并定义compareTo
方法。
一个对象的集合根据compareTo
方法以自然排序进行排序。
像Integer
、String
和Date
这样的Java
类实现了Comparable
接口并覆盖了其compareTo
方法,它们以词表顺序(lexicographic-order
)进行排序。
从Java
源代码中找到naturalOrder
方法声明。
static <T extends Comparable<? super T>> Comparator<T> naturalOrder()
- 1
为了扭转自然排序,我们可以使用Comparator.reverseOrder
方法。
在本页面中,我们将提供一些例子,说明如何使用Comparator.naturalOrder
与Stream.sorted
、Collections.sort
、List.sort
和Arrays.sort
一起对对象的集合进行自然排序。
与 Stream.sorted 一起使用
Stream.sorted
返回一个由这个流的元素组成的流,根据提供的比较器进行排序。
StreamSortedDemo.java
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class StreamSortedDemo {
public static void main(String[] args) {
List<Integer> numList = Arrays.asList(12, 10, 15, 8, 11);
numList.stream().sorted(Comparator.naturalOrder()).forEach(n -> System.out.print(n + " "));
System.out.println("\n-----------");
List<String> strList = Arrays.asList("Varanasi", "Allahabad", "Kanpur", "Noida");
strList.stream().sorted(Comparator.naturalOrder()).forEach(s -> System.out.print(s + " "));
System.out.println("\n-----------");
List<Student> stdList = Student.getStudentList();
stdList.stream().sorted(Comparator.naturalOrder()).forEach(s -> System.out.print(s.getName() + " "));
System.out.println("\n-----------"); //reverse order of natural order using Comparator.reverseOrder()
stdList = Student.getStudentList();
stdList.stream().sorted(Comparator.reverseOrder()).forEach(s -> System.out.print(s.getName() + " "));
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
Student.java
import java.util.Arrays;
import java.util.List;
public class Student implements Comparable<Student> {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public int compareTo(Student s) {
return name.compareTo(s.getName());
}
public static List<Student> getStudentList() {
Student s1 = new Student("Ram", 18);
Student s2 = new Student("Shyam", 22);
Student s3 = new Student("Mohan", 19);
Student s4 = new Student("Mahesh", 20);
Student s5 = new Student("Krishna", 21);
List<Student> list = Arrays.asList(s1, s2, s3, s4, s5);
return list;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
输出
8 10 11 12 15
-----------
Allahabad Kanpur Noida Varanasi
-----------
Krishna Mahesh Mohan Ram Shyam
-----------
Shyam Ram Mohan Mahesh Krish
- 1
- 2
- 3
- 4
- 5
- 6
- 7
与 Collections.sort 一起使用
Collections.sort
根据给定的比较器实例对指定的列表进行排序。
CollectionsSortDemo.java
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class CollectionsSortDemo {
public static void main(String[] args) {
List<Integer> numList = Arrays.asList(12, 10, 15, 8, 11);
Collections.sort(numList, Comparator.naturalOrder());
numList.forEach(n -> System.out.print(n + " "));
System.out.println("\n-----------");
List<String> strList = Arrays.asList("Varanasi", "Allahabad", "Kanpur", "Noida");
Collections.sort(strList, Comparator.naturalOrder());
strList.forEach(s -> System.out.print(s + " "));
System.out.println("\n-----------");
List<Student> stdList = Student.getStudentList();
Collections.sort(stdList, Comparator.naturalOrder());
stdList.forEach(s -> System.out.print(s.getName() + " "));
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
与 List.sort 一起使用
List.sort
根据给定的比较器实例对这个列表进行排序。
ListSortDemo.java
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class ListSortDemo {
public static void main(String[] args) {
List<Integer> numList = Arrays.asList(12, 10, 15, 8, 11);
numList.sort(Comparator.naturalOrder());
numList.forEach(n -> System.out.print(n + " "));
System.out.println("\n-----------");
List<String> strList = Arrays.asList("Varanasi", "Allahabad", "Kanpur", "Noida");
strList.sort(Comparator.naturalOrder());
strList.forEach(s -> System.out.print(s + " "));
System.out.println("\n-----------");
List<Student> stdList = Student.getStudentList();
stdList.sort(Comparator.naturalOrder());
stdList.forEach(s -> System.out.print(s.getName() + " "));
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
与 Arrays.sort 一起使用
Arrays.sort
根据指定的比较器引起的顺序对指定的对象阵列进行排序。
ArraysSortDemo.java
import java.util.Arrays;
import java.util.Comparator;
public class ArraysSortDemo {
public static void main(String[] args) {
Student s1 = new Student("Ram", 18);
Student s2 = new Student("Shyam", 22);
Student s3 = new Student("Mohan", 19);
Student[] stdArray = { s1, s2, s3 };
Arrays.sort(stdArray, Comparator.naturalOrder());
for (Student s : stdArray) {
System.out.print(s.getName() + " ");
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
参考文献
【1】Interface Comparator
【2】Java 8 Stream sorted() Example
【3】Java Comparator.reverseOrder
【4】Java Comparator.naturalOrder