1Z0-829 Stream.max(Comparator) Practice Question
Which code will successfully produce an Optional<Integer> that contains the maximum value from a list of integers?
⚠ Common exam trap
Oracle tests that Stream.max(Comparator) and Collectors.maxBy(Comparator) both return Optional<T>. Also, calling max() without a Comparator on a Stream<T> does not compile; this is distinct from IntStream.max().
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
B and D both produce Optional<Integer>.
Option A is correct because it states that options B and D both produce Optional<Integer>. Option B uses list.stream().collect(Collectors.maxBy(Integer::compareTo)), which returns an Optional<Integer> containing the maximum element. Option D uses list.stream().max(Integer::compareTo), which also returns an Optional<Integer> with the maximum element. Option C, list.stream().max() without arguments, fails to compile because the Stream.max() method requires a Comparator. Therefore, A is the correct choice.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
B and D both produce Optional<Integer>.
Why this is correct
Both B and C compile and produce an Optional<Integer> with the maximum element.
- ✗
list.stream().collect(Collectors.maxBy(Integer::compareTo))
Why it's wrong here
This is also correct, but not the only one.
- ✗
list.stream().max()
Why it's wrong here
max() without a Comparator does not compile; Stream.max() requires a Comparator argument.
- ✗
list.stream().max(Integer::compareTo)
Why it's wrong here
This is correct, but is not the only correct answer.
Go deeper
Related to this question
About these practice questions
This 1Z0-829 question is part of Courseiva's 513-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This 1Z0-829 practice question is part of Courseiva's free Oracle certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 1Z0-829 exam.