Program to convert Primitive Array to Stream in Java
An array is a group of like-typed variables that are referred to by a common name. An array can contain primitives data types as well as objects of a class depending on the definition of the array. In case of primitives data types, the actual values are stored in contiguous memory locations. In case of objects of a class, the actual objects are stored in heap segment.
A Primitive array is an array which is defined with the help of the primitive wrapper classes, instead of objects.
Example: Integer a = new Integer(4);
When a Primitive Array is converted into a Stream, primitive Streams will be obtained like IntStream, DoubleStream and LongStream.
Examples:
Input: Double Array: [1.2, 2.4, 3.6, 4.8, 5.0]
Output: DoubleStream: [1.2, 2.4, 3.6, 4.8, 5.0]Input: Integer Array: [1, 2, 3, 4, 5]
Output: IntStream: [1, 2, 3, 4, 5]
Below are methods to convert Primitive Array to Stream in Java:
- Using Arrays.stream():
Algorithm:
- Get the Array to be converted.
- Convert the array into Stream using Arrays.stream() method by passing the array as the parameter.
- Return the formed Stream
Program:
// Java Program to convert// Array to Streamimportjava.util.*;importjava.util.stream.*;classGFG {// Generic function to convert// an Array to StreampublicstaticIntStreamconvertArrayToStream(intarray[]){// Return the converted StreamreturnArrays.stream(array);}publicstaticvoidmain(String args[]){// Create an Arrayint[] array =newint[] {3,2,5,4,1};// Print the ArraySystem.out.println("Array: "+ Arrays.toString(array));// convert the Array to StreamIntStream stream = convertArrayToStream(array);// Print the StreamSystem.out.println("Stream: "+ Arrays.toString(stream.toArray()));}}chevron_rightfilter_noneOutput:Array: [3, 2, 5, 4, 1] Stream: [3, 2, 5, 4, 1]
- Using IntStream.of(): The IntStream.of() method creates a Stream directly with the primitive values or collection passed as the parameter.
Algorithm:
- Get the Array to be converted.
- Convert the array into Stream using IntStream.of() method by passing the array as the parameter.
- Return the formed Stream
Program:
// Java Program to convert// Array to Streamimportjava.util.*;importjava.util.stream.*;classGFG {// Generic function to convert// an Array to StreampublicstaticIntStreamconvertArrayToStream(intarray[]){// Return the converted StreamreturnIntStream.of(array);}publicstaticvoidmain(String args[]){// Create an Arrayint[] array =newint[] {3,2,5,4,1};// Print the ArraySystem.out.println("Array: "+ Arrays.toString(array));// convert the Array to StreamIntStream stream = convertArrayToStream(array);// Print the StreamSystem.out.println("Stream: "+ Arrays.toString(stream.toArray()));}}chevron_rightfilter_noneOutput:Array: [3, 2, 5, 4, 1] Stream: [3, 2, 5, 4, 1]
Recommended Posts:
- Program to Convert Stream to an Array in Java
- Program to convert Boxed Array to Stream in Java
- Program to convert a Map to a Stream in Java
- Program to convert a Set to Stream in Java using Generics
- Program to Convert List to Stream in Java
- How to get slice of a Primitive Array in Java
- Is an array a primitive type or an object in Java?
- Convert a Set to Stream in Java
- Convert Stream to Set in Java
- Convert an Iterable to Stream in Java
- Convert an Iterator to Stream in Java
- Program to convert Array to Set in Java
- Program to convert Array to List in Java
- Java Program to convert Character Array to IntStream
- Program to convert Byte Array to Writer in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



