Is there a sane way to get an ordered stream from a list (array list specifically, but it shouldn't matter) that streams elements in reverse of how they are in the original list?
I'm looking for a solution that doesn't involve buffering data in anything (collector, another list, array, etc, because they copy the container which is wasteful), or uses Collections.reverse
(because it modifies the list).
So far, the cleanest ways that I see here is to implement my own version of Spliterator
that's ORDERED
and advances through the list in reverse, or implement an Iterator
that iterates in reverse, and use Spliterators.spliteratorUnknownSize(iterator,ORDERED)
on it.
Note this question is different from Java 8 stream reverse order : that other question asks on how to reverse a stream (which is impossible in general case), and answers offer to reverse the source somehow (which I don't want to do), and then stream that reversed source. The cost of reversing the source is O(N), and I want to avoid it at all if possible.