1. Overview
1.概述
In this tutorial, we’ll see different ways to check if a list is sorted in Java.
在本教程中,我们将看到在Java中检查一个列表是否排序的不同方法。
2. Iterative Approach
2.迭代方法
The iterative approach is a simple and intuitive way to check for a sorted list. In this approach, we’ll iterate the list and compare the adjacent elements. If any of the two adjacent elements are not sorted, we can say that the list is not sorted.
迭代法是检查排序列表的一种简单而直观的方法。在这种方法中,我们将对列表进行迭代,比较相邻的元素。如果任何一个相邻的元素没有被排序,我们可以说这个列表没有被排序。
A list can be either sorted in the natural order or in a custom order. We’ll cover both these cases using Comparable and Comparator interfaces.
一个列表既可以按自然顺序排序,也可以按自定义顺序排序。我们将使用 Comparable 和 Comparator 接口涵盖这两种情况。
2.1. Using Comparable
2.1.使用Comparable
First, let’s see an example of a list whose elements are of type Comparable. Here, we’ll consider a list containing objects of type String:
首先,让我们看看一个元素类型为Comparable的列表的例子。在这里,我们将考虑一个包含String类型对象的列表。
public static boolean isSorted(List<String> listOfStrings) {
if (isEmpty(listOfStrings) || listOfStrings.size() == 1) {
return true;
}
Iterator<String> iter = listOfStrings.iterator();
String current, previous = iter.next();
while (iter.hasNext()) {
current = iter.next();
if (previous.compareTo(current) > 0) {
return false;
}
previous = current;
}
return true;
}
2.2. Using Comparator
2.2.使用比较器
Now, let’s consider an Employee class, which does not implement Comparable. So, in this case, we need to use a Comparator to compare the adjacent elements of the list:
现在,让我们考虑一个Employee类,它没有实现Comparable。所以,在这种情况下,我们需要使用一个Comparator来比较列表中相邻的元素。
public static boolean isSorted(List<Employee> employees, Comparator<Employee> employeeComparator) {
if (isEmpty(employees) || employees.size() == 1) {
return true;
}
Iterator<Employee> iter = employees.iterator();
Employee current, previous = iter.next();
while (iter.hasNext()) {
current = iter.next();
if (employeeComparator.compare(previous, current) > 0) {
return false;
}
previous = current;
}
return true;
}
The above two examples are similar. The only difference is in how we compare the previous and the current elements of the list.
上面的两个例子是相似的。唯一的区别是我们如何比较列表中的前一个和当前的元素。
In addition, we can also use Comparator to have precise control over the sorting check. Further information about these two is available in our Comparator and Comparable in Java tutorial.
此外,我们还可以使用Comparator来对排序检查进行精确控制。关于这两者的进一步信息,可以在我们的Java中的Comparator和Comparable 教程中找到。
3. Recursive Approach
3.递归方法
Now, we’ll see how to check for a sorted list using recursion:
现在,我们将看到如何使用递归来检查一个排序的列表。
public static boolean isSorted(List<String> listOfStrings) {
return isSorted(listOfStrings, listOfStrings.size());
}
public static boolean isSorted(List<String> listOfStrings, int index) {
if (index < 2) {
return true;
} else if (listOfStrings.get(index - 2).compareTo(listOfStrings.get(index - 1)) > 0) {
return false;
} else {
return isSorted(listOfStrings, index - 1);
}
}
4. Using Guava
4.使用番石榴
It’s often good to use a third-party library instead of writing our own logic. The Guava library has some utility classes that we can use to check if a list is sorted.
使用第三方库而不是自己编写逻辑往往是好事。Guava库有一些实用类,我们可以用它们来检查一个列表是否被排序。
4.1. Guava Ordering Class
4.1.Guava Ordering 类
In this section, we’ll see how to use the Ordering class in Guava to check for a sorted list.
在本节中,我们将看到如何使用Guava中的Ordering类来检查一个排序的列表。
First, we’ll see an example of a list containing elements of type Comparable:
首先,我们将看到一个包含Comparable类型元素的列表的例子。
public static boolean isSorted(List<String> listOfStrings) {
return Ordering.<String> natural().isOrdered(listOfStrings);
}
Next, we’ll see how we can check if a list of Employee objects is sorted using a Comparator:
接下来,我们将看到如何使用Comparator来检查Employee对象的列表是否被排序。
public static boolean isSorted(List<Employee> employees, Comparator<Employee> employeeComparator) {
return Ordering.from(employeeComparator).isOrdered(employees);
}
Also, we can use natural().reverseOrder() to check if a list is sorted in reverse order. In addition, we can use natural().nullFirst() and natural().nullLast() to check if null appears to the first or the last of the sorted list.
另外,我们可以使用natural().reverseOrder()来检查一个列表是否是按相反的顺序排序的。此外,我们可以使用natural().nullFirst()和natural().nullLast()来检查null是否出现在排序后的列表的第一个或最后一个。
To know more about Guava Ordering class, we can refer our Guide to Guava’s Ordering article.
要了解更多关于Guava Ordering类的信息,我们可以参考我们的Guava的订购指南文章。
4.2. Guava Comparators Class
4.2.Guava Comparators 类
If we are using Java 8 or above, Guava provides a better alternative in terms of Comparators class. We’ll see an example of using the isInOrder method of this class:
如果我们使用的是Java 8或以上版本,Guava在Comparators类方面提供了一个更好的选择。我们将看到一个使用该类的isInOrder方法的例子。
public static boolean isSorted(List<String> listOfStrings) {
return Comparators.isInOrder(listOfStrings, Comparator.<String> naturalOrder());
}
As we can see, in the above example, we’ve used the natural ordering to check for a sorted list. We can also use a Comparator to customize the sorting check.
我们可以看到,在上面的例子中,我们使用了自然排序来检查一个排序的列表。我们也可以使用Comparator来定制排序检查。
5. Conclusion
5.结论
In this article, we’ve seen how we can check for a sorted list using a simple iterative approach, a recursive approach, and using Guava. We’ve also briefly touched upon the usage of Comparator and Comparable in deciding the sorting check logic.
在这篇文章中,我们已经看到了如何使用简单的迭代方法、递归方法和使用Guava来检查一个排序的列表。我们还简单地谈到了Comparator和Comparable在决定排序检查逻辑中的用法。
The implementation of all these examples and code snippets can be found over on GitHub.
所有这些例子和代码片断的实现都可以在GitHub上找到over。