There are following way to reverse an array in java 8.
- Intstream.rangeClosed() method iterate the numbers from 1 to array length.
- Inside map we reverse the array(a.length-n) and convert to array.
- Convert the array using Array.toString and print the array data.
import java.util.Arrays;
import java.util.stream.IntStream;
public class ReverseArrayTest {
public static void main(String[] args) {
int[] a = { 2, 3, 1, 1, 4, 3, 2, 5 };
int[] value = IntStream.rangeClosed(1, a.length).map(n -> a[a.length - n]).toArray();
System.out.println("Reverse array value is :- " + Arrays.toString(value));
}
}
Output :-
Reverse array value is :- [5, 2, 3, 4, 1, 1, 3, 2]