Govt. Exams
Entrance Exams
map() is an intermediate operation that returns an IntStream. The lambda expression (x -> x * 2) transforms each int, but map() itself returns IntStream, not the transformed value type.
Optional with map() using a lambda expression is the functional programming approach. It's safe, readable, and chains operations effectively. Options B, C, and D are imperative approaches.
If a lambda body throws a checked exception, it must be declared in the functional interface's method signature. Otherwise, compilation will fail.
Lambda expressions can only be used with functional interfaces (single abstract method), whereas anonymous classes can implement any interface. Lambda expressions are also more concise.
Both expressions correctly sort by length. Option B uses Integer.compare (safer for int overflow), while Option C uses direct subtraction. Both work correctly here.
Function f1 = x -> x * 2;
Function f2 = x -> x + 5;
How to apply f1 first, then f2?
Both andThen and explicit composition order result in applying f1 first (3*2=6, then 6+5=11), equivalent to option C.
map() is an intermediate operation. The println executes because collect() is a terminal operation that triggers evaluation. It prints 1, 2, 3 (the original values).
This is a curried function - a higher-order function that takes one argument and returns another function taking the next argument. It transforms multi-argument functions into sequences of single-argument functions.
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.
This is a curried function. curry.apply(5) returns a function that adds 5. Applying that with 3 gives 5 + 3 = 8.