简体   繁体   中英

Stream array in reverse order / Java

Reversing an ordered stream

Does streaming an array and then reversing the order of the stream result in the overhead (eg like the necessity to fully stream the array first and then iterate on it backwards) ?

Using a helper structure

final Object[] arr; // assume this is filled
List<Integer> list = Arrays.asList(arr);
Collections.reverse(list);
list.stream();

Is the only way to circumvent overhead (eg like the creation of an additional List) to iterate over the array in reverse order?

Does streaming an array and then reversing the order of the stream result in the overhead

Yes.

Is the only way to circumvent overhead (eg like the creation of an additional List) to iterate over the array in reverse order?

Yes, do it with an IntStream :

IntStream.range(0, arr.length).mapToObj(i -> arr[arr.length-1-i])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM