Given:
Which statement on line 1 enables this code to compile?
Given:
Which statement on line 1 enables this code to compile?
The forEach method accepts a Consumer as its argument. A proper implementation of a Consumer that prints each element of the list is: Consumer<String> function = (String f) -> System.out.println(f); The other options involve different functional interfaces such as Supplier, Predicate, and Function, which are not appropriate for use with forEach.
In fact, the above answers are all wrong A seems correct, but the writing in lambda is wrong If rewritten as # Consumer function = s -> System.out.println(s); to compile normally
A is correct cause foreach accept consumer interface
NOTICE: A is the correct but consumer is bad implement in the option because explicit paramater for lambda is (String f)->... and the compiler expect Object, the correct is : 1- Consumer x = x -> System.out.print(x) or: 2- Consumer<String> x = (String f) -> ..... o 3- Consumer<String> x= f -> sout...
Actually answer A is also incorrect. As Consumer is actually Consumer<Object> so it expects an Object rather than a String. It would've worked if it were: f -> System.out.println(f);
Answer:A
forEach() accepts a Consumer, correct answer is A.
forEach() accepts a Consumer, so correct answer is A.
forEach() accepts a Consumer, so correct answer is A. Consumer function = f -> System.out.println(f);