Junior — Middle
How to convert a Stream back to a List within the Stream API?
sobes.tech AI
Answer from AI
To convert a Stream back into a List in Java, the collect method with the collector Collectors.toList() is used. This is the most common and convenient way.
Example:
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Example {
public static void main(String[] args) {
Stream<String> stream = Stream.of("apple", "banana", "cherry");
List<String> list = stream.collect(Collectors.toList());
System.out.println(list); // [apple, banana, cherry]
}
}