Question 19
Back to Exercise
What is the output of the following Java program?
import java.util.List;
record Page<T>(List<T> items, int page, int totalPages) {
boolean hasNext() { return page < totalPages; }
<R> Page<R> map(java.util.function.Function<T, R> f) { return new Page<>(items.stream().map(f).toList(), page, totalPages); }
}
public class Main {
public static void main(String[] args) {
Page<String> p = new Page<>(List.of("java", "go"), 1, 3);
Page<Integer> lengths = p.map(String::length);
System.out.println(p.hasNext() + " " + lengths + " " + new Page<>(List.of(), 3, 3).hasNext());
}
}
- A true Page[items=[4, 2], page=1, totalPages=3] false Correct
- B false Page[items=[4, 2], page=1, totalPages=3] false
- C true Page[items=[java, go], page=1, totalPages=3] false
- D Compilation error
Correct Answer: Option A
Records can be generic and have generic methods.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!