Write a program to reverse a Stream?

Here we can do this in multiple ways. But without using a intermediate storage will be a better way to solve this problem:

 

Approach#1

IntStream reverse(int from, int to) {
    return IntStream.range(from, to).map(i -> to - i + from - 1);
}

reverse(0, 5).toArray();
output:
[4,3,2,1,0]




Leave a comment