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 Stream
Â
Âimport
java.util.*;
import
java.util.stream.*;
Â
Âclass
GFG {
Â
ÂÂ Â Â Â
// Generic function to convertÂ
   Â
// an Array to Stream
   Â
public
static
IntStreamÂ
               Â
convertArrayToStream(
int
array[])
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
// Return the converted Stream
       Â
return
Arrays.stream(array);
   Â
}
Â
ÂÂ Â Â Â
public
static
void
main(String args[])
   Â
{
       Â
// Create an Array
       Â
int
[] array =
new
int
[] {
3
,
2
,
5
,
4
,
1
};
Â
ÂÂ Â Â Â Â Â Â Â
// Print the Array
       Â
System.out.println(
"Array: "
ÂÂ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
+ Arrays.toString(array));
Â
ÂÂ Â Â Â Â Â Â Â
// convert the Array to Stream
       Â
IntStream stream = convertArrayToStream(array);
Â
ÂÂ Â Â Â Â Â Â Â
// Print the Stream
       Â
System.out.println(
"Stream: "
ÂÂ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
+ Arrays.toString(stream.toArray()));
   Â
}
}
Output: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 Stream
Â
Âimport
java.util.*;
import
java.util.stream.*;
Â
Âclass
GFG {
Â
ÂÂ Â Â Â
// Generic function to convertÂ
   Â
// an Array to Stream
   Â
public
static
IntStreamÂ
              Â
convertArrayToStream(
int
array[])
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
// Return the converted Stream
       Â
return
IntStream.of(array);
   Â
}
Â
ÂÂ Â Â Â
public
static
void
main(String args[])
   Â
{
       Â
// Create an Array
       Â
int
[] array =
new
int
[] {
3
,
2
,
5
,
4
,
1
};
Â
ÂÂ Â Â Â Â Â Â Â
// Print the Array
       Â
System.out.println(
"Array: "
ÂÂ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
+ Arrays.toString(array));
Â
ÂÂ Â Â Â Â Â Â Â
// convert the Array to Stream
       Â
IntStream stream = convertArrayToStream(array);
Â
ÂÂ Â Â Â Â Â Â Â
// Print the Stream
       Â
System.out.println(
"Stream: "
ÂÂ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
+ Arrays.toString(stream.toArray()));
   Â
}
}
Output:Array: [3, 2, 5, 4, 1] Stream: [3, 2, 5, 4, 1]