In this source code example, we will see how to use Java 8 stream API to create a Stream instance from an Array with an example.
Creating a Stream object from Arrays
Array can be a source of a Stream or Array can be created from the existing array or of a part of an array:
importjava.io.IOException;importjava.util.Arrays;importjava.util.stream.Stream;publicclassStreamCreationExamples {publicstaticvoidmain(String[]args)throwsIOException {// Array can also be a source of a StreamStream<String> streamOfArray=Stream.of("a","b","c"); streamOfArray.forEach(System.out::println);// creating from existing array or of a part of an array:String[] arr=newString[] {"a","b","c" };Stream<String> streamOfArrayFull=Arrays.stream(arr); streamOfArrayFull.forEach(System.out::println);Stream<String> streamOfArrayPart=Arrays.stream(arr,1,3); streamOfArrayPart.forEach(System.out::println); }}
Output:
abcabcbc
Comments
Post a Comment