This article contains different ways for Java 8 convert Array to Stream i.e. Stream.of and Array.stream using those methods, Array can be converted in Stream to perform iteration easily.

Example 1: Array to Stream using Stream.of

Employee[] employeesArray=new Employee[2];
        employeesArray[0]=  new Employee(1, "Jone");
        employeesArray[1]=  new Employee(2, "Shone");

        Stream.of(employeesArray)                         // Stream.of of java.util.Stream is static method to convert array to stream
        .forEach(System.out::println);

Output

1-Jone
2-Shone

Example 2: Arrays to Steam using Arrays.stream

Employee[] employeesArray=new Employee[2];
        employeesArray[0]=  new Employee(1, "Jone");
        employeesArray[1]=  new Employee(2, "Shone");

 Arrays.stream(employeesArray).forEach(System.out::println);  // Arrays.stream is static method of java.util.Arrays to convert Array to Stream

Output

1-Jone
2-Shone

References:

https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html

 

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No

Leave a Reply

Your email address will not be published. Required fields are marked *