Program to convert Primitive Array to Stream in Java
The article covers the Java feature that lets developers apply stream operations to primitive data types, such as integers, doubles, and booleans, by converting primitive arrays into streams.
Example 1:
Input: double[] doubleArray = {1.1, 2.2, 3.3, 4.4, 5.5};
Output:
1.1 2.2 3.3 4.4 5.5
Example 2:
Input: int[] intArray = {1, 2, 3, 4, 5};
Output:
1 2 3 4 5
There are multiple ways to turn primitive arrays into streams in Java, each appropriate for a particular use case. Here are a few typical methods such as :
1. Using Arrays.stream() method
Primitive arrays can be converted to streams using the above approach, which returns an IntStream, LongStream, DoubleStream, or Stream and provides overloaded versions for int, long, double, and boolean types.
Here is the implementation of using arrays.stream() method,
FileName:PrimitiveArrayToStream.java
import java.util.Arrays; import java.util.stream.DoubleStream; import java.util.stream.IntStream; import java.util.stream.LongStream; import java.util.stream.Stream; public class PrimitiveArrayToStream { public static void main(String[] args) { // Example with int array int[] intArray = {1, 2, 3, 4, 5}; IntStream intStream = Arrays.stream(intArray); intStream.forEach(System.out::println); // Example with long array long[] longArray = {1L, 2L, 3L, 4L, 5L}; LongStream longStream = Arrays.stream(longArray); longStream.forEach(System.out::println); // Example with double array double[] doubleArray = {1.1, 2.2, 3.3, 4.4, 5.5}; DoubleStream doubleStream = Arrays.stream(doubleArray); doubleStream.forEach(System.out::println); // Example with boolean array boolean[] booleanArray = {true, false, true}; StreambooleanStream = createBooleanStream(booleanArray); booleanStream.forEach(System.out::println); } // Convert boolean array to Stream private static Stream createBooleanStream(boolean[] array) { Stream.Builder builder = Stream.builder(); for (boolean value : array) { builder.accept(value); } return builder.build(); } }
Output:
1 2 3 4 5 1 2 3 4 5 1.1 2.2 3.3 4.4 5.5 true false true
Complexity analysis: The code has a time complexity of O(n) for creating streams from primitive arrays and printing elements, and a space complexity of O(n) due to the additional memory required for storing wrapper objects or the stream builder object and boolean values.
2. Using Stream.of() method
Stream.of() creates a stream by converting arrays of int, long, double, and boolean types into an array of their corresponding wrapper classes, giving back a Stream, Stream, Stream, or Stream.
FileName:PrimitiveArrayToStream1.java
import java.util.Arrays; import java.util.stream.Stream; public class PrimitiveArrayToStream1 { public static void main(String[] args) { // Example with int array int[] intArray = {1, 2, 3, 4, 5}; StreamintStream = Stream.of(Arrays.stream(intArray).boxed().toArray(Integer[]::new)); intStream.forEach(System.out::println); // Example with long array long[] longArray = {1L, 2L, 3L, 4L, 5L}; Stream longStream = Stream.of(Arrays.stream(longArray).boxed().toArray(Long[]::new)); longStream.forEach(System.out::println); // Example with double array double[] doubleArray = {1.1, 2.2, 3.3, 4.4, 5.5}; Stream doubleStream = Stream.of(Arrays.stream(doubleArray).boxed().toArray(Double[]::new)); doubleStream.forEach(System.out::println); // Example with boolean array boolean[] booleanArray = {true, false, true}; Stream booleanStream = createBooleanStream(booleanArray); booleanStream.forEach(System.out::println); } // Convert boolean array to Stream private static Stream createBooleanStream(boolean[] array) { Stream.Builder builder = Stream.builder(); for (boolean value : array) { builder.add(value); } return builder.build(); } }
Output:
1 2 3 4 5 1 2 3 4 5 1.1 2.2 3.3 4.4 5.5 true false true
Complexity analysis: The code has a time complexity of O(n) for processing each array and a space complexity of O(n) for storing the wrapper objects or stream builder.
3. Using Arrays.stream() with Range
For int arrays, use IntStream.range() or IntStream.rangeClosed() to create a stream of consecutive integers within a given range.
FileName:PrimitiveArrayToStream2.java
import java.util.stream.IntStream; public class PrimitiveArrayToStream2 { public static void main(String[] args) { // Example with IntStream.range() IntStream rangeStream = IntStream.range(1, 6); // Generates stream of integers from 1 to 5 rangeStream.forEach(System.out::println); // Example with IntStream.rangeClosed() IntStream rangeClosedStream = IntStream.rangeClosed(1, 5); // Generates a stream of integers from 1 to 5 (inclusive) rangeClosedStream.forEach(System.out::println); } }
Output:
1 2 3 4 5 1 2 3 4 5
Complexity analysis: The code has a time complexity of O(n) for each range operation and a space complexity of O(1), as no additional memory is required beyond the input parameters.
4. Using Stream.Builder
For primitive types, we can use Stream. Builder to manually construct a stream. Although it requires more code, it also offers greater flexibility.
FileName:PrimitiveArrayToStream3.java
import java.util.stream.DoubleStream; import java.util.stream.Stream; public class PrimitiveArrayToStream3 { public static void main(String[] args) { double[] doubleArray = {1.1, 2.2, 3.3, 4.4, 5.5}; // Using Arrays.stream() to convert double[] to DoubleStream DoubleStream doubleStream = DoubleStream.of(doubleArray); // Using Stream.Builderto manually build a stream Stream.Builder doubleStreamBuilder = Stream.builder(); for (double d : doubleArray) { doubleStreamBuilder.accept(d); } Stream streamFromBuilder = doubleStreamBuilder.build(); System.out.println("Stream created using Arrays.stream():"); doubleStream.forEach(System.out::println); System.out.println(" Stream created using Stream.Builder:"); streamFromBuilder.forEach(System.out::println); } }
Output:
Stream created using Arrays.stream(): 1.1 2.2 3.3 4.4 5.5 Stream created using Stream.Builder: 1.1 2.2 3.3 4.4 5.5
Complexity analysis: The code has a time complexity of O(n) for each operation and a space complexity of O(n) for storing the wrapper objects or the stream builder.