What is the difference between Comparator :: reverseOrder and Comparator.reverseOrder () when used in a sorted stream method

What is the difference between Comparator::reverseOrderand Comparator.reverseOrder()when used in a sorted stream method.

    Stream<String> streamText = Stream.of("over the river", "through the woods", "to grandmother house we go");

It works:

    streamText.filter(n -> n.startsWith("t"))
         .sorted(Comparator.reverseOrder())
         .findFirst().ifPresent(System.out::println);

But this does not compile:

    streamText.filter(n -> n.startsWith("t"))
         .sorted(Comparator::reverseOrder)
         .findFirst().ifPresent(System.out::println);
+4
source share
1 answer

Good question!

sortedneed a Comparator<T>, right? Comparator<T>- functional interface. It is a function that takes 2 arguments and returns intindicating which argument is greater or equal.

Comparator.reverseOrder(), reverseOrder - , a Comparator<T>. , Comparator, sorted. .

Comparator::reverseOrder reverseOrder. reverseOrder sorted. , sorted , 2 int, reverseOrder, Comparator<T>. . ?

: , 2 int OR a Comparator<T>

: , Comparator<T>

.

+9

Source: https://habr.com/ru/post/1696302/


All Articles