1Z0-829 Working with Streams and Lambda Expressions Practice Question
A developer uses a parallel stream to process a large collection and wants to collect results into a List while preserving encounter order. Which of the following collectors will guarantee order preservation?
⚠ Common exam trap
Many candidates assume `Collectors.toList()` always preserves encounter order, but the Java specification explicitly states it does not guarantee order in parallel streams, and the exam tests this subtle distinction.
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
✓
collect(ArrayList::new, List::add, List::addAll)
The custom collector using `ArrayList::new`, `List::add`, and `List::addAll` explicitly specifies an insertion-ordered collection (`ArrayList`) and uses the combiner `List::addAll`, which preserves the encounter order when merging partial results from parallel streams. The `Collectors.toList()` and `Collectors.toUnmodifiableList()` do not guarantee order preservation in parallel streams, as their internal implementations may use non-ordered structures like `HashSet` during accumulation.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
collect(Collectors.toUnmodifiableList())
Why it's wrong here
Similar to toList(), order is not guaranteed.
- ✗
collect(Collectors.toList())
Why it's wrong here
Collectors.toList() does not guarantee encounter order in a parallel stream.
- ✗
collect(Collectors.toMap(Function.identity(), v->v))
Why it's wrong here
toMap produces a Map which is unordered by nature.
- ✓
collect(ArrayList::new, List::add, List::addAll)
Why this is correct
This custom collector preserves encounter order because it adds elements sequentially in the accumulator and merges lists in order.
- ✗
collect(Collectors.toCollection(TreeSet::new))
Why it's wrong here
TreeSet sorts elements, not preserve original order.
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.