函数 Collections.reverseOrder() 的作用是什么?
What does the function Collections.reverseOrder() do?
int min=Collections.min(list, Collections.reverseOrder());
执行reverseOrder
函数后会输出什么?
java.util.Collections.reverseOrder()
方法是java.util.Collections
class方法。
根据 javadoc:
Returns a comparator that imposes the reverse of the natural ordering
on a collection of objects that implement the Comparable
interface.
The natural ordering is the ordering imposed by the objects' own
compareTo method public static Comparator reverseOrder()
您可以在以下站点上找到其他一些很好的示例:
https://www.geeksforgeeks.org/collections-reverseorder-java-examples/
在您的情况下,以下表达式是等效的:
Collections.min(list, Collections.reverseOrder())
Collections.max(list)
这是一个例子,这个函数只是简单地反转你的数字并且它们很低。查看这个例子,希望你能理解它的作用。
import java.util.*;
public class HelloWorld{
public static void main(String []args){
List<Integer> nums = Arrays.asList(1, 10, 5, 23, 2, 3);
nums.stream()
.forEach(System.out::println);
nums.stream()
.sorted(Comparator.reverseOrder())
.forEach(System.out::println);
}
}
有时候最好自己看看源代码。
public static <T> Comparator<T> reverseOrder() {
return (Comparator<T>) ReverseComparator.REVERSE_ORDER;
}
REVERSE_ORDER
只是 ReverseComparator
内部 class.
的一个对象
static final ReverseComparator REVERSE_ORDER = new ReverseComparator();
这似乎是关于 Collections 的面试问题:
现在,Collections.reverseOrder
是一个颠倒正常顺序的 Comparator
,>
变成 <
,反之亦然。
所以下面的说法是一样的:
int max = Collections.min(list, Collections.reverseOrder());
int max = Collections.max(list);
结果是最大值。
没有真正的速度惩罚,因为列表本身没有被修改,列表没有被复制。 List
的复杂度是O(N):需要遍历所有元素。
在可行的情况下,使用 TreeSet
之类的 SortedSet
而不是 List
会更好。
int min=Collections.min(list, Collections.reverseOrder());
执行reverseOrder
函数后会输出什么?
java.util.Collections.reverseOrder()
方法是java.util.Collections
class方法。
根据 javadoc:
Returns a comparator that imposes the reverse of the natural ordering on a collection of objects that implement the
Comparable
interface. The natural ordering is the ordering imposed by the objects' own compareTo methodpublic static Comparator reverseOrder()
您可以在以下站点上找到其他一些很好的示例: https://www.geeksforgeeks.org/collections-reverseorder-java-examples/
在您的情况下,以下表达式是等效的:
Collections.min(list, Collections.reverseOrder())
Collections.max(list)
这是一个例子,这个函数只是简单地反转你的数字并且它们很低。查看这个例子,希望你能理解它的作用。
import java.util.*;
public class HelloWorld{
public static void main(String []args){
List<Integer> nums = Arrays.asList(1, 10, 5, 23, 2, 3);
nums.stream()
.forEach(System.out::println);
nums.stream()
.sorted(Comparator.reverseOrder())
.forEach(System.out::println);
}
}
有时候最好自己看看源代码。
public static <T> Comparator<T> reverseOrder() {
return (Comparator<T>) ReverseComparator.REVERSE_ORDER;
}
REVERSE_ORDER
只是 ReverseComparator
内部 class.
static final ReverseComparator REVERSE_ORDER = new ReverseComparator();
这似乎是关于 Collections 的面试问题:
现在,Collections.reverseOrder
是一个颠倒正常顺序的 Comparator
,>
变成 <
,反之亦然。
所以下面的说法是一样的:
int max = Collections.min(list, Collections.reverseOrder());
int max = Collections.max(list);
结果是最大值。
没有真正的速度惩罚,因为列表本身没有被修改,列表没有被复制。 List
的复杂度是O(N):需要遍历所有元素。
在可行的情况下,使用 TreeSet
之类的 SortedSet
而不是 List
会更好。