In this tutorial, we will see “How to convert an Array to a Stream in Java 8?”
To achieve that, we are going to use Arrays.stream OR Stream.of operations available since Java 8.
Convert an Array to a Stream in Java 8 with example...!!! Click To Tweet
Table of Contents
- Example 1. Convert Array of String to Stream using Arrays.stream
- Example 2. Convert Array of String to Stream using Stream.of
- Example 3. Convert Array of Integer to Stream using Arrays.stream
- Example 4. Convert Array of Integer to Stream using Stream.of (It won’t work – check the solution)
- Example 5. Convert Array of Integer to Stream using Arrays.stream
- Example 6. Convert Array of Integer to Stream using Stream.of (It won’t work – check the solution)
Example 1. Convert Array of String to Stream using Arrays.stream
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import java.util.Arrays; import java.util.stream.Stream; public class Convert_Array_To_Stream_Java8_Example1 { public static void main(String[] args) { String[] arrayObj = { "Tom", "Henry", "Dan", "Zack", "John" }; //Arrays.stream Stream <String> streamObj1 = Arrays.stream(arrayObj); System.out.println("Stream of strings using Arrays.stream:"); streamObj1.forEach(s -> System.out.println(s)); } } |
Output:
1 2 3 4 5 6 | Stream of strings using Arrays.stream: Tom Henry Dan Zack John |
Example 2. Convert Array of String to Stream using Stream.of
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import java.util.stream.Stream; public class Convert_Array_To_Stream_Java8_Example2 { public static void main(String[] args) { String[] arrayObj = { "Tom", "Henry", "Dan", "Zack", "John" }; //Stream.of Stream <String> streamObj2 = Stream.of(arrayObj); System.out.println("Stream of strings using Stream.of:"); streamObj2.forEach(s -> System.out.println(s)); } } |
Output:
1 2 3 4 5 6 | Stream of strings using Stream.of: Tom Henry Dan Zack John |
Example 3. Convert Array of Integer to Stream using Arrays.stream
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import java.util.Arrays; import java.util.stream.IntStream; public class Convert_Array_To_Stream_Java8_Example3 { public static void main(String[] args) { int[] arrayObj = { 10, 62, 93, 84, 75 }; //Arrays.stream IntStream intStreamObj1 = Arrays.stream(arrayObj); System.out.println("IntStream of integers using Arrays.stream:"); intStreamObj1.forEach(x -> System.out.println(x)); } } |
Output:
1 2 3 4 5 6 | IntStream of integers using Arrays.stream: 10 62 93 84 75 |
Example 4. Convert Array of Integer to Stream using Stream.of (It won’t work – check the solution)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | import java.util.Arrays; import java.util.stream.IntStream; import java.util.stream.Stream; public class Convert_Array_To_Stream_Java8_Example4 { public static void main(String[] args) { int[] arrayObj = { 10, 62, 93, 84, 75 }; //Stream.of (It won't work for integers) Stream <int[]> streamObj1 = Stream.of(arrayObj); System.out.println("Stream of integers using Stream.of:"); //It won't work as you can't print Stream<int[]> directly streamObj1.forEach(s -> System.out.println(s)); //To make it working, convert the Stream<int[]> into IntStream first using flatMapToInt Stream <int[]> streamObj2 = Stream.of(arrayObj); IntStream intStreamObj2 = streamObj2.flatMapToInt(i -> Arrays.stream(i)); System.out.println("\nIntStream of integers using Stream.of:"); intStreamObj2.forEach(s -> System.out.println(s)); } } |
Output:
1 2 3 4 5 6 7 8 9 | Stream of integers using Stream.of: [I@816f27d IntStream of integers using Stream.of: 10 62 93 84 75 |
Example 5. Convert Array of Integer to Stream using Arrays.stream
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import java.util.Arrays; import java.util.stream.Stream; public class Convert_Array_To_Stream_Java8_Example5 { public static void main(String[] args) { String[] arrayObj = { "Tom", "Henry", "Dan", "Zack", "John" }; //Arrays.stream Stream <String> streamObj1 = Arrays.stream(arrayObj, 2, 5); System.out.println("Stream of strings within range 2 and 5 using Arrays.stream:"); streamObj1.forEach(s -> System.out.println(s)); } } |
Output:
1 2 3 4 | Stream of strings within range 2 and 5 using Arrays.stream: Dan Zack John |
Example 6. Convert Array of Integer to Stream using Stream.of (It won’t work – check the solution)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import java.util.Arrays; import java.util.stream.IntStream; import java.util.stream.Stream; public class Convert_Array_To_Stream_Java8_Example6 { public static void main(String[] args) { int[] arrayObj = { 10, 62, 93, 84, 75 }; //Arrays.stream IntStream streamObj1 = Arrays.stream(arrayObj, 2, 5); System.out.println("Stream of integers within range 2 and 5 using Arrays.stream:"); streamObj1.forEach(s - > System.out.println(s)); } } |
Output:
1 2 3 4 | Stream of integers within range 2 and 5 using Arrays.stream: 93 84 75 |
Convert an Array to a Stream in Java 8 with example...!!! Click To Tweet
Do you like this Post? – then check my other helpful posts:
Other Useful References: