Q 61
Medium
What is the difference between Stream.collect() and Collection methods for bulk operations?
Answer
Collection bulk methods (addAll, removeAll, retainAll) modify the target collection in place synchronously. Stream.collect() constructs a new result structure declaratively using functional pipelines without mutating the source collection.
Explanation
Streams enable functional transformations (filter, map) and easy parallel processing via parallelStream().
Code Example
Java
List<Integer> list = List.of(1, 2, 3, 4, 5);
List<Integer> evenSquares = list.stream()
.filter(n -> n % 2 == 0)
.map(n -> n * n)
.collect(Collectors.toList());
Reference:
Java Collections Framework