For the specific question of generating a reverse IntStream, try something like this:

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

This avoids boxing and sorting.

For the general question of how to reverse a stream of any type, I don't know of there's a "proper" way. There are a couple ways I can think of. Both end up storing the stream elements. I don't know of a way to reverse a stream without storing the elements.

This first way stores the elements into an array and reads them out to a stream in reverse order. Note that since we don't know the runtime type of the stream elements, we can't type the array properly, requiring an unchecked cast.

@SuppressWarnings("unchecked")
static <T> Stream<T> reverse(Stream<T> input) {
    Object[] temp = input.toArray();
    return (Stream<T>) IntStream.range(0, temp.length)
                                .mapToObj(i -> temp[temp.length - i - 1]);
}

Another technique uses collectors to accumulate the items into a reversed list. This does lots of insertions at the front of ArrayList objects, so there's lots of copying going on.

Stream<T> input = ... ;
List<T> output =
    input.collect(ArrayList::new,
                  (list, e) -> list.add(0, e),
                  (list1, list2) -> list1.addAll(0, list2));

It's probably possible to write a much more efficient reversing collector using some kind of customized data structure.

UPDATE 2016-01-29

Since this question has gotten a bit of attention recently, I figure I should update my answer to solve the problem with inserting at the front of ArrayList. This will be horribly inefficient with a large number of elements, requiring O(N^2) copying.

It's preferable to use an ArrayDeque instead, which efficiently supports insertion at the front. A small wrinkle is that we can't use the three-arg form of Stream.collect(); it requires the contents of the second arg be merged into the first arg, and there's no "add-all-at-front" bulk operation on Deque. Instead, we use addAll() to append the contents of the first arg to the end of the second, and then we return the second. This requires using the Collector.of() factory method.

The complete code is this:

Deque<String> output =
    input.collect(Collector.of(
        ArrayDeque::new,
        (deq, t) -> deq.addFirst(t),
        (d1, d2) -> { d2.addAll(d1); return d2; }));

The result is a Deque instead of a List, but that shouldn't be much of an issue, as it can easily be iterated or streamed in the now-reversed order.

Answer from Stuart Marks on Stack Overflow
Top answer
1 of 16
116

For the specific question of generating a reverse IntStream, try something like this:

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

This avoids boxing and sorting.

For the general question of how to reverse a stream of any type, I don't know of there's a "proper" way. There are a couple ways I can think of. Both end up storing the stream elements. I don't know of a way to reverse a stream without storing the elements.

This first way stores the elements into an array and reads them out to a stream in reverse order. Note that since we don't know the runtime type of the stream elements, we can't type the array properly, requiring an unchecked cast.

@SuppressWarnings("unchecked")
static <T> Stream<T> reverse(Stream<T> input) {
    Object[] temp = input.toArray();
    return (Stream<T>) IntStream.range(0, temp.length)
                                .mapToObj(i -> temp[temp.length - i - 1]);
}

Another technique uses collectors to accumulate the items into a reversed list. This does lots of insertions at the front of ArrayList objects, so there's lots of copying going on.

Stream<T> input = ... ;
List<T> output =
    input.collect(ArrayList::new,
                  (list, e) -> list.add(0, e),
                  (list1, list2) -> list1.addAll(0, list2));

It's probably possible to write a much more efficient reversing collector using some kind of customized data structure.

UPDATE 2016-01-29

Since this question has gotten a bit of attention recently, I figure I should update my answer to solve the problem with inserting at the front of ArrayList. This will be horribly inefficient with a large number of elements, requiring O(N^2) copying.

It's preferable to use an ArrayDeque instead, which efficiently supports insertion at the front. A small wrinkle is that we can't use the three-arg form of Stream.collect(); it requires the contents of the second arg be merged into the first arg, and there's no "add-all-at-front" bulk operation on Deque. Instead, we use addAll() to append the contents of the first arg to the end of the second, and then we return the second. This requires using the Collector.of() factory method.

The complete code is this:

Deque<String> output =
    input.collect(Collector.of(
        ArrayDeque::new,
        (deq, t) -> deq.addFirst(t),
        (d1, d2) -> { d2.addAll(d1); return d2; }));

The result is a Deque instead of a List, but that shouldn't be much of an issue, as it can easily be iterated or streamed in the now-reversed order.

2 of 16
76

Elegant solution

List<Integer> list = Arrays.asList(1,2,3,4);
list.stream()
    .sorted(Collections.reverseOrder()) // Method on Stream<Integer>
    .forEach(System.out::println);
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java array โ€บ how to reverse an array in java
How to Invert an Array in Java | Baeldung
January 8, 2024 - Here we use the method IntStream.range to generate a sequential stream of numbers. Then we map this sequence into array indexes in descending order. Letโ€™s see how to invert an array using the Collections.reverse() method:
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ java-8-streams-collect-and-reverse-stream-into-list
Java 8 Streams - Collect and Reverse Stream into List
December 13, 2021 - If you'd like to read more about Functional Interfaces and Lambda Expressions in Java - read our Guide to Functional Interfaces and Lambda Expressions in Java! ... In this short guide, we've taken a look at how you can collect and reverse a stream/list in Java 8, using collect() and Collections.reverse() - individually, and together through the collectingAndThen() method.
๐ŸŒ
Don&#039;t Panic!
dontpanicblog.co.uk โ€บ home โ€บ reverse order a stream in java
Reverse order a Stream in Java - Don't Panic!
April 7, 2024 - The simplest way to get round this is to collect() the Stream to a data structure (for example, a List) and then reverse the collected data structure.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ reverse-elements-of-a-parallel-stream-in-java
Reverse elements of a Parallel Stream in Java - GeeksforGeeks
July 11, 2025 - Collector.of() : The idea is to create a collector that accumulates elements of the specified Stream into an ArrayDeque or ArrayList in reverse order Algorithm: Get the parallel stream.
๐ŸŒ
Level Up Lunch
leveluplunch.com โ€บ java โ€บ tutorials โ€บ 028-reverse-order-stream-elements-java8
Reverse elements of stream java 8 | Level Up Lunch
February 23, 2015 - Since a LinkedList is a double-linked data structure we are able to navigate in either direction, forward or reverse. Calling DescendingIterator allows us to iterate over the elements in reverse sequential order.
๐ŸŒ
Java Ice Breakers
javaicebreakers.com โ€บ 2022 โ€บ 05 โ€บ 11 โ€บ reverse-array-using-stream
How to reverse all the numbers in an array of integers using stream โ€“ Java Ice Breakers
May 7, 2023 - This is the third solution to reverse all the numbers in the given array. This solution uses IntStream, range and map methods. import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.function.Supplier; import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.Stream; public class ReverseArrayNumbersUSingStream { public static void main(String[] args) { int [] arrNums= {10,2,41,13,8,6,7,15,1}; System.out.println("Array values before reverse- below"); Arrays.stream(arrNums).forEach(x->System.out.print(x+" ")); System.out.println("\nArray values after reverse- below"); //Map each index to the element of the array in the reverse order IntStream.range(0, arrNums.length) .map(i -> arrNums[arrNums.length - i - 1]) .forEach(e->System.out.print(e+" ")); } }
๐ŸŒ
Software Testing Help
softwaretestinghelp.com โ€บ home โ€บ java โ€บ how to reverse an array in java: 3 methods with examples
How to Reverse An Array In Java: 3 Methods With Examples
March 24, 2020 - Learn how to reverse an array in Java using 3 simple methods with examples. Methods like using loops, Collections, and built-in methods with code explanations.
Find elsewhere
๐ŸŒ
Attacomsian
attacomsian.com โ€บ blog โ€บ java-reverse-stream-elements
How to reverse the elements of a stream in Java
October 8, 2022 - // create a simple Stream of strings Stream<String> stream = Stream.of("Alex", "John", "Baray", "Emma"); // reverse stream and print elements stream.collect(Collectors.toCollection(LinkedList::new)) .descendingIterator().forEachRemaining(System.out::println); In the above example, we first created a Stream of string and then collect the elements into a LinkedList. Since the LinkedList is a double-linked data structure in Java, we can iterate it in any direction: forward and backward.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java list โ€บ reverse an arraylist in java
Reverse an ArrayList in Java | Baeldung
April 3, 2025 - The Java standard library has provided the Collections.reverse method to reverse the order of the elements in the given List. This convenient method does in-place reversing, which will reverse the order in the original list it received.
๐ŸŒ
Tutorials Freak
tutorialsfreak.com โ€บ java-tutorial โ€บ examples โ€บ reverse-array
How to Reverse an Array in Java? Array Reverse Program
Learn how to reverse an array in Java with this comprehensive array reverse program. Understand this technique with practical examples. Get Started Now!
๐ŸŒ
How to do in Java
howtodoinjava.com โ€บ home โ€บ java array โ€บ reverse an array in java
Reverse an Array in Java
December 6, 2022 - Learn to reverse an array using for-loop, swapping items, Collections API and also the Apache Commons's ArrayUtils class.
๐ŸŒ
Quora
quora.com โ€บ In-Java-8-how-can-we-reverse-a-single-string-using-lambdas-and-streams
In Java 8, how can we reverse a single string using lambdas and streams? - Quora
Answer (1 of 5): I donโ€™t know why you need a Lambda here. You can just do - [code]String abc = "abc"; System.out.println(new StringBuilder(abc).reverse().toString()); [/code]But if you do want to use Stream and Lambda you can do something like - [code]String abc = "abc"; String reverse = Arrays...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ reverse-an-array-in-java
Reverse an Array in Java - GeeksforGeeks
July 11, 2025 - When we are working with a String array, we can use a StringBuilder and append each array element with a for loop decrementing from the array's length, then convert the StringBuilder to a string, and split back into an array.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-iterate-array-in-reverse-order
Java - Iterate Array in Reverse Order - GeeksforGeeks
July 23, 2025 - // Java Program to iterate over an array // in reverse order using Streams API import java.util.stream.IntStream; public class Main { public static void main(String[] args) { // Array initialization int[] arr = {10, 20, 30, 40, 50}; // Iterating over the array in reverse // order using Streams IntStream.range(0, arr.length) .map(i -> arr[arr.length - i - 1]) .forEach(i -> System.out.print(i + " ")); } }
๐ŸŒ
Upgrad
upgrad.com โ€บ home โ€บ tutorials โ€บ software & tech โ€บ reverse an array in java
Comprehensive Guide to Reversing an Array in Java
April 1, 2025 - Collections.reverse(): Perfect for ArrayList instances. A new array: Generates a backward copy of the initial array. Java 8 Stream: Beneficial for functional programming styles.
๐ŸŒ
Java Code Geeks
examples.javacodegeeks.com โ€บ home โ€บ java development โ€บ core java
Reverse Array Java Example - Java Code Geeks
February 7, 2022 - You can also use Apache Commons ArrayUtils.reverse() method to reverse any array in Java. This method is overloaded to reverse byte, short, long, int, float, double and String array.
๐ŸŒ
FavTutor
favtutor.com โ€บ blogs โ€บ reverse-list-java
8 Ways to Reverse a List in Java | FavTutor
January 22, 2022 - There is a major issue with this approach, i.e., the new list returned by the reverse method is backed up by the original list. It means that we change the reversed list, then the original list will also be affected. To get around this problem, we can create a new list containing the contents of the list returned by the reverse method. This approach makes use of the stream API.