Arrays.stream
时间: 2023-10-24 15:09:26 浏览: 86
Arrays.stream() is a method in Java that creates a stream from an array. It is part of the Java 8 Stream API and is used to perform operations on arrays in a functional programming style.
Syntax:
Arrays.stream(array)
Parameters:
array - the array that needs to be converted into a stream.
Return Value:
It returns a stream of the array elements.
Example:
int[] arr = {1, 2, 3, 4, 5};
IntStream stream = Arrays.stream(arr);
stream.forEach(System.out::println);
This code will create a stream from the int array and then print each element of the stream using the forEach() method. The output will be:
1
2
3
4
5
Note: The Arrays.stream() method can be used with arrays of any primitive type (byte, short, int, long, float, double, and boolean) as well as with arrays of objects.
阅读全文