To convert a 2D array to a 1D stream in Java, you can use the Arrays.stream
method to create a stream from the 2D array, and then use the flatMapToInt
method to flatten the stream into a 1D stream.
Here is an example of how you can convert a 2D array to a 1D stream in Java:
int[][] array2D = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; IntStream stream = Arrays.stream(array2D).flatMapToInt(Arrays::stream);
In this example, we have a 2D array called array2D
that contains three rows of integers. We use the Arrays.stream
method to create a stream from the 2D array, and then use the flatMapToInt
method to flatten the stream into a 1D stream of integers.
You can then use the various methods of the IntStream
class to perform operations on the stream, such as filtering, mapping, or reducing. For example, you can use the filter
method to select only the even numbers from the stream, or the mapToInt
method to multiply each number by 2.