Java 8 stream reverse order

General question: What’s the proper way to reverse a stream? Assuming that we don’t know what type of elements that stream consists of, what’s the generic way to reverse any stream?

Specific question:

IntStream provides range method to generate Integers in specific range IntStream.range(-range, 0), now that I want to reverse it switching range from 0 to negative won’t work, also I can’t use Integer::compare

List<Integer> list = Arrays.asList(1,2,3,4);
list.stream().sorted(Integer::compare).forEach(System.out::println);

with IntStream I’ll get this compiler error

Error:(191, 0) ajc: The method sorted() in the type IntStream is not applicable for the arguments (Integer::compare)

what am I missing here?

30 Answers
30

Leave a Comment