Arrays are one of the most commonly used data structures in Java. With the introduction of the Stream API in Java 8, arrays can be easily converted into streams, allowing developers to perform filtering, mapping, sorting, and other operations in a functional and concise way.
- Converts arrays into streams for functional-style data processing.
- Supports both object arrays and primitive arrays.
- Enables powerful operations such as filter(), map(), sorted(), reduce(), and collect().
Methods to Convert an Array to a Stream
Java provides multiple ways to convert an array into a stream depending on the array type.
1. Convert an Array to a Stream using Arrays.stream()
The Arrays.stream() method is the most commonly used way to create a stream from an array. It works with both object arrays and primitive arrays.
Syntax:
Arrays.stream(array);
Example using Arrays.stream() to convert string array to stream.
The Arrays.stream() method creates a stream from a String array, and each element is displayed using the forEach() terminal operation.
import java.util.*;
import java.util.stream.*;
class GFG {
public static void main(String[] args)
{
// Converting String array to stream
String[] arr = { "Geeks", "for", "Geeks" };
// Using Arrays.stream to convert an
// array into Stream
Stream<String> stm = Arrays.stream(arr);
// Displaying elements in Stream
stm.forEach(str -> System.out.print(str + " "));
}
}
Output
Geeks for Geeks
Example Using Arrays.stream() to convert long and double arrays to stream.
Arrays.stream() converts long and double arrays into their respective primitive streams, allowing each element to be processed efficiently.
import java.util.*;
import java.util.stream.*;
class GFG {
public static void main(String[] args)
{ // Converting long array to stream
long[] arrL = { 3L, 5L, 6L, 8L, 9L };
LongStream stmL = Arrays.stream(arrL);
stmL.forEach(number -> System.out.print(number + " "));
System.out.println();
// Converting double array to stream
double[] arrD = { 1, 2, 3, 4, 5 };
DoubleStream stmD = Arrays.stream(arrD);
stmD.forEach(d -> System.out.print(d + " "));
}
}
Output:
Output
3 5 6 8 9 1.0 2.0 3.0 4.0 5.0
2. Convert an Array to a Stream using Stream.of()
The Stream.of() method creates a stream from an object array. It is suitable for arrays of reference types such as String, Integer, or custom objects.
Syntax:
Stream.of(array);
Note : For object arrays, Stream.of() internally uses Arrays.stream().
Example using Stream.of( ) to convert string array to stream.
Stream.of() creates a stream from a String array, and the stream elements are processed one by one using forEach().
import java.util.*;
import java.util.stream.*;
class GFG {
public static void main(String[] args)
{
// Converting String array to stream
String[] arr = { "Geeks", "for", "Geeks" };
// Using Arrays.stream to convert an
// array into Stream
Stream<String> stm = Stream.of(arr);
// Displaying elements in Stream
stm.forEach(str -> System.out.print(str + " "));
}
}
Output
Geeks for Geeks
Example using Stream.of( ) to convert long and double arrays to stream.
LongStream.of() and DoubleStream.of() create streams directly from primitive arrays, which are then traversed and printed
import java.util.*;
import java.util.stream.*;
class GFG {
public static void main(String[] args)
{ // Converting long array to stream
long[] arrL = { 3L, 5L, 6L, 8L, 9L };
LongStream stmL = LongStream.of(arrL);
stmL.forEach(number -> System.out.print(number + " "));
System.out.println();
// Converting double array to stream
double[] arrD = { 1, 2, 3, 4, 5 };
DoubleStream stmD = DoubleStream.of(arrD);
stmD.forEach(d -> System.out.print(d + " "));
}
}
Output
3 5 6 8 9 1.0 2.0 3.0 4.0 5.0
3. Convert an Array to a Stream using Arrays.spliterator()
A Spliterator is an object used to traverse and partition elements efficiently. It can be converted into a stream using StreamSupport.stream().
Syntax:
StreamSupport.stream(
Arrays.spliterator(array), false
);
Example using Arrays.spliterator() to Convert a String Array to a Stream
A spliterator is created from the array and then converted into a sequential stream using StreamSupport.stream().
import java.util.*;
import java.util.stream.*;
class GFG {
public static void main(String[] args)
{
// Converting String array to Spliterator
String[] arr = { "Geeks", "for", "Geeks" };
// Creating stream using Spliterator
Stream<String> stm = StreamSupport.stream(
Arrays.spliterator(arr), false);
// Displaying stream elements
stm.forEach(str -> System.out.print(str + " "));
}
}
Output
Geeks for Geeks
Example using Arrays.spliterator() to Convert an Integer Array to a Stream
The array is first converted into a Spliterator, which is then used to create a stream for processing its elements.
import java.util.*;
import java.util.stream.*;
class GFG {
public static void main(String[] args)
{
Integer[] arr = { 10, 20, 30, 40, 50 };
Stream<Integer> stm = StreamSupport.stream(
Arrays.spliterator(arr), false);
stm.forEach(num -> System.out.print(num + " "));
}
}
Output
10 20 30 40 50
Common Operations After Converting an Array to a Stream
After converting an array into a stream, you can perform various intermediate and terminal operations to process its elements efficiently. These operations allow you to filter, transform, sort, aggregate, and collect data in a concise and functional manner.
- filter(): Selects elements that satisfy a given condition.
- map(): Transforms each element into another form.
- sorted(): Arranges elements in their natural or custom order.
- distinct(): Removes duplicate elements from the stream.
- limit(): Restricts the stream to a specified number of elements.
- skip(): Skips the specified number of elements.
- reduce(): Combines stream elements into a single result.
- collect(): Collects stream elements into a collection or another data structure.
- forEach(): Performs an action on each stream element.