Which of the following are true? (Choose all that apply.)
private static void magic(Stream<Integer> s) { Optional o = s.filter(x -> x < 5).limit(3).max((x, y) -> x-y); System.out.println(o.get()); }
empty()
); runs infinitely.empty()
); throws an exception.B, F.
Calling get()
on an empty Optional causes an exception to be thrown, making options B and F correct.
Option C is incorrect because the infinite stream is made finite by the intermediate limit()
operation.
Options A and E are incorrect because the source streams are not infinite.
Therefore, the call to max()
sees only three elements and terminates.