Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse Sort a stream

I want to reverse sort a stream such as below but getting compile time error as "The method sorted() in the type IntStream is not applicable for the arguments ((<no type> o1, <no type> o2) -> {})". Can anyone correct this

IntStream.range(1, 100)
         .filter(x -> x%2 != 0)
         .sorted((o1,o2) -> -o1.compareTo(o2))
         .forEach(System.out::println);
like image 760
Sundresh Avatar asked Dec 13 '18 15:12

Sundresh


People also ask

How do I sort in reverse order in stream?

Sort the stream in reverse order using Stream. sorted() method by passing Comparator. reverseOrder() to it that imposes the reverse of the natural ordering.

How do you reverse sort in Java?

To sort an array in Java in descending order, you have to use the reverseOrder() method from the Collections class. The reverseOrder() method does not parse the array. Instead, it will merely reverse the natural ordering of the array.

How do you sort a list in reverse order in Java using stream?

To sort in reverse order, use Comparator. reverseOrder() in sorted() method.

How do I sort a list in reverse order in Java 8?

To get the list in the reverse order, we need to use Collections. reverseOrder() and Collections. sort() methods together.


2 Answers

Explanation

since you're working with an IntStream it only has one overload of the sorted method and it's the natural order (which makes sense).

instead, box the stream from IntStream to Stream<Integer> then it should suffice:

 IntStream.range(1, 100)
          .filter(x -> x % 2 != 0)
          .boxed()  // <--- boxed to Stream<Integer>
          .sorted((o1,o2) -> -o1.compareTo(o2)) // now we can call compareTo on Integer
          .forEach(System.out::println);

However, as mentioned by @Holger in the comments:

never use a comparator function like (o1,o2) -> -o1.compareTo(o2). It is perfectly legal for a compareTo implementation to return Integer.MIN_VALUE, in which case negation will fail and produce inconsistent results. A valid reverse comparator would be (o1,o2) -> o2.compareTo(o1)

Suggestions to improve your code. JDK8

A better approach would be:

IntStream.range(1, 100)
         .filter(x -> x % 2 != 0)
         .boxed()
         .sorted(Comparator.reverseOrder())
         .forEachOrdered(System.out::println);

why?

  • There's already a built-in comparator to perform reverse order as shown above.
  • if you want to guarantee that the elements are to be seen in the sorted order when printing then utilise forEachOrdered (big shout out to @Holger for always reminding me)

or as suggested by @Holger it can all be simplified to as little as this:

 IntStream.range(0, 50)
          .map(i -> 99-i*2)
          .forEachOrdered(System.out::println);

JDK9 variant

btw, in JDK9 starting from the code in your post, you can first simplify it via iterate to:

IntStream.iterate(1, i  -> i <= 99, i -> i + 2)
         .boxed()
         .sorted(Comparator.reverseOrder())
         .forEachOrdered(System.out::println);

With this approach, we can avoid the filter intermediate operation and increment in 2's.

and finally you could simplify it even further with:

IntStream.iterate(99, i  -> i > 0 , i -> i - 2)
         .forEachOrdered(System.out::println);

With this approach, we can avoid filter, boxed,sorted et al.

like image 74
Ousmane D. Avatar answered Sep 27 '22 18:09

Ousmane D.


If that is your real code, then it may be more efficient to use IntStream.iterate and generate numbers from 99 to 0:

IntStream.iterate(99, i -> i - 1)
    .limit(100)
    .filter(x -> x % 2 != 0)
    .forEachOrdered(System.out::println);
like image 21
ernest_k Avatar answered Sep 27 '22 19:09

ernest_k