Java SE 8 Programmer II — Question 26
Given the code fragment:
List<Integer> nums = Arrays.asList (10, 20, 8):
System.out.println (
//line n1
);
line n1 to enable the code to print the maximum number in the nums list?
Which code fragment must be inserted at
Answer options
- A. nums.stream().max(Comparator.comparing(a -> a)).get()
- B. nums.stream().max(Integer : : max).get()
- C. nums.stream().max()
- D. nums.stream().map(a -> a).max()
Correct answer: C
Explanation
The correct answer, C, utilizes the max() method directly on the stream, which is designed to find the maximum value. Options A and B introduce unnecessary complexity with comparators, while D maps the elements but does not find the maximum directly, making them unsuitable for this task.