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...!!! Share on X
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
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:
Stream of strings using Arrays.stream: Tom Henry Dan Zack John
Example 2. Convert Array of String to Stream using Stream.of
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:
Stream of strings using Stream.of: Tom Henry Dan Zack John
Example 3. Convert Array of Integer to Stream using Arrays.stream
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:
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)
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:
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
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:
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)
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:
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...!!! Share on X
Do you like this Post? – then check my other helpful posts:
Other Useful References: