create stream from array java 1

create stream from array java

// import java.util.Arrays;
// import java.util.stream.Stream;

String[] array = {"a", "b", "c", "d", "e"};

//Arrays.stream
Stream stream1 = Arrays.stream(array);
stream1.forEach(x -> System.out.println(x));

//Stream.of
Stream stream2 = Stream.of(array);
stream2.forEach(x -> System.out.println(x));

Here is what the above code is Doing:
1. We create an array of String objects.
2. We create a stream from the array using the Arrays.stream() method.
3. We iterate over the stream using the forEach() method.
4. We create another stream from the array using the Stream.of() method.
5. We iterate over the stream using the forEach() method.

Similar Posts