Given:
IntStream stream = IntStream.of (1,2,3);
IntFunction
IntStream newStream = stream.map(inFu.apply(10)); //line n2 newStream.forEach(System.output::print);
Which modification enables the code fragment to compile?
Given:
IntStream stream = IntStream.of (1,2,3);
IntFunction
IntStream newStream = stream.map(inFu.apply(10)); //line n2 newStream.forEach(System.output::print);
Which modification enables the code fragment to compile?
To ensure the code fragment compiles correctly, line n1 should be replaced with 'IntFunction<IntUnaryOperator> inFu = x -> y -> x * y;'. This defines 'inFu' as a function that takes an integer and returns an 'IntUnaryOperator'. The 'IntUnaryOperator' is a functional interface that takes an int and returns an int, which matches the expected behavior for the 'map' method in the IntStream class.
B, tested, but I think we need to change (System.output::print) to (System.out::print)
Correct answer is B,tested.
Correct answer is B.
B is the answer: import java.util.function.IntFunction; import java.util.function.IntUnaryOperator; import java.util.stream.IntStream; public class Main { public static void main(String[] args) { IntStream stream = IntStream.of(1, 2, 3); IntFunction<IntUnaryOperator> inFu = x -> y -> x * y; // Replace with option B IntStream newStream = stream.map(inFu.apply(10)); // line n2 newStream.forEach(System.out::print); } }
The full of implement is IntFunction<IntUnaryOperator> inFu = new IntFunction<IntUnaryOperator>() { @Override public IntUnaryOperator apply(int value) { return new IntUnaryOperator() { @Override public int applyAsInt(int operand) { return value * operand; } }; } }; And it is a IntStream so map function will take IntUnaryOperation. So the inFu.apply(10) is correct return the IntUnaryOperation so the B is correct
Answer is definitely B. This is an example of higher order function where the function interface returns another function.
Correct answer is B. The function.apply() expects IntFunction<IntUnaryOperator>