Java OCA OCP Practice Question 3097

Question

Given the code segment:

List<Integer> integers = Arrays.asList(15, 5, 10, 20, 25, 0);
// GETMAX

Which of the code segments can be replaced for the comment marked with GETMAX to return the maximum value?.

a)Integer max = integers.stream().max((i, j) -> i - j).get();
b)Integer max = integers.stream().max().get();
c)Integer max = integers.max();
d)Integer max = integers.stream().mapToInt(i -> i).max();


a)

Note

Calling stream() method on a List<Integer> object results in a stream of type Stream<Integer>.

the max() method takes a Comparator as the argument that is provided by the lambda expression (i, j) -> i - j.

the max() method returns an Optional<Integer> and the get() method returns an Integer value.

option b) the max() method in Stream requires a Comparator to be passed as the argument

option c) there is no max() method in List<Integer>

option d) the mapToInt() method returns an IntStream, but the max() method returns an OptionalInt and hence it cannot be assigned to Integer (as required in this context).




PreviousNext

Related