What is the time complexity of reducing a stream with a stateful lambda operation?
AO(n) for sequential, O(log n) for parallel
BO(n) for both sequential and parallel streams
CO(1) regardless of stream size
DDepends on the operation and stream characteristics
Correct Answer:
D. Depends on the operation and stream characteristics
EXPLANATION
Time complexity depends on the specific reduction operation, whether the lambda is stateless, and stream characteristics. Generally O(n), but parallelization overhead affects actual performance.
Consider: List list = Arrays.asList("a", "b", "c"); list.stream().collect(Collectors.toMap(s -> s, s -> s.length())). What type is returned?
AMap
BMap
CList
DSet
Correct Answer:
A. Map
EXPLANATION
Collectors.toMap with key mapper (s -> s) produces String keys and value mapper (s -> s.length()) produces Integer values, resulting in Map<String, Integer>.
What is the time complexity of reducing a stream with a lambda expression using reduce()?
AO(n) in sequential, potentially O(log n) in parallel with proper associative operation
BAlways O(1)
CO(n²) regardless of parallelism
DO(n log n) always
Correct Answer:
A. O(n) in sequential, potentially O(log n) in parallel with proper associative operation
EXPLANATION
Sequential reduce is O(n). Parallel reduction can be O(log n) with a properly associative and commutative operation, enabling divide-and-conquer approach.
A method processes collections where items need to be added back to the same collection type. Which wildcard approach ensures type safety for write operations?
Apublic static void addElements(List
Bpublic static void addElements(List destination, List source)
Cpublic static void addElements(List destination, List source)
Dpublic static void addElements(List destination, List source)
Correct Answer:
A. public static void addElements(List
EXPLANATION
'? super T' allows writing T instances (contravariant), while '? extends T' allows safe reading (covariant). This combination enables flexible yet type-safe collection manipulation.