Junior — Middle
What object is returned after applying the filter method to a stream from an ArrayList?
sobes.tech AI
Answer from AI
After applying the filter method to a stream from an ArrayList in Java, a new object of type Stream<T> is returned, where T is the type of the elements of the original list.
The filter method does not modify the original stream but creates an intermediate stream that contains only those elements that satisfy the given condition (predicate). This allows chaining operations on streams without immediate execution.
Example:
List<String> list = Arrays.asList("apple", "banana", "cherry");
Stream<String> filteredStream = list.stream().filter(s -> s.startsWith("b"));
// filteredStream contains elements starting with 'b'
filteredStream.forEach(System.out::println); // Will print: banana