Junior — Middle
Is it possible to convert an array into a data stream in programming language?
sobes.tech AI
Answer from AI
Yes, in Java, you can convert an array into a stream using the Stream API introduced in Java 8.
Example of converting an array to a stream and processing it:
import java.util.Arrays;
public class ArrayToStreamExample {
public static void main(String[] args) {
String[] array = {"apple", "banana", "cherry"};
Arrays.stream(array)
.filter(s -> s.startsWith("b"))
.forEach(System.out::println); // prints "banana"
}
}
The method Arrays.stream(array) creates a stream of array elements, which can then be processed with various operations like filtering, transforming, and collecting.