To convert a Java 8 Stream
to an array, you can use the Stream.toArray()
method with an array generator to specify the type of the resulting array. This approach ensures type safety and avoids intermediate collections. Below are detailed explanations and examples:
1. Using Stream.toArray(IntFunction<A[]> generator)
The most efficient and concise method is to use toArray
with a method reference to the array constructor.
Syntax
T[] array = stream.toArray(T[]::new);
Examples
import java.util.stream.Stream;
public class StreamToArrayExample {
public static void main(String[] args) {
// Example 1: Convert a Stream of Strings to a String[]
Stream<String> stringStream = Stream.of("Apple", "Banana", "Cherry");
String[] stringArray = stringStream.toArray(String[]::new);
System.out.println(Arrays.toString(stringArray)); // [Apple, Banana, Cherry]
// Example 2: Convert a Stream of Integers to an Integer[]
Stream<Integer> integerStream = Stream.of(1, 2, 3);
Integer[] integerArray = integerStream.toArray(Integer[]::new);
System.out.println(Arrays.toString(integerArray)); // [1, 2, 3]
// Example 3: Empty Stream
Stream<String> emptyStream = Stream.empty();
String[] emptyArray = emptyStream.toArray(String[]::new);
System.out.println(Arrays.toString(emptyArray)); // []
}
}
2. Handling Primitive Streams (IntStream, LongStream, DoubleStream)
For primitive streams, use the dedicated toArray()
method:
import java.util.stream.IntStream;
public class PrimitiveStreamExample {
public static void main(String[] args) {
// Convert an IntStream to an int[]
IntStream intStream = IntStream.of(10, 20, 30);
int[] intArray = intStream.toArray();
System.out.println(Arrays.toString(intArray)); // [10, 20, 30]
}
}
3. Using Collectors (Alternative Approach)
While less efficient, you can collect the stream into a List
and then convert it to an array:
import java.util.stream.*;
import java.util.*;
public class CollectorsExample {
public static void main(String[] args) {
Stream<String> stream = Stream.of("Dog", "Cat", "Bird");
List<String> list = stream.collect(Collectors.toList());
String[] array = list.toArray(new String[0]);
System.out.println(Arrays.toString(array)); // [Dog, Cat, Bird]
}
}
Key Considerations
- Type Safety: Use
toArray(T[]::new)
to avoidObject[]
results. - Performance: Directly converting with
toArray
is more efficient than using intermediate collections. - Empty Streams: Returns an empty array of the specified type.
- Parallel Streams: Works seamlessly with parallel streams.
Common Mistakes
Mistake 1: Using toArray()
Without a Generator
This returns an Object[]
, which may require casting:
Stream<String> stream = Stream.of("A", "B", "C");
Object[] objArray = stream.toArray(); // Not type-safe!
Mistake 2: Incorrect Array Type
Ensure the generator matches the stream’s type:
Stream<Integer> stream = Stream.of(1, 2, 3);
String[] array = stream.toArray(String[]::new); // Compilation error!
Summary
- Use
stream.toArray(T[]::new)
for type-safe conversion. - Use primitive stream
toArray()
forint[]
,long[]
, ordouble[]
. - Avoid intermediate steps (e.g., converting to a
List
) for better performance.