Question 357 of 513
1Z0-829 Working with Arrays and Collections Practice Question
A company's Java 17 application processes large log files and stores word counts. Initially, they used a TreeMap<String, Integer> to maintain sorted word counts. After adding 10 million entries, insertion performance became unacceptably slow. The team switched to a HashMap<String, Integer> for fast insertions, but now they need to produce sorted reports. They are considering two approaches: (1) Keep the HashMap and, when a sorted report is needed, extract all entries into an ArrayList<Map.Entry<String, Integer>> and sort it using Collections.sort with a comparator, or (2) Use a ConcurrentSkipListMap instead. The application is single-threaded, and reports are requested infrequently. What is the best course of action?
⚠ Common exam trap
Many exam-takers assume sorted data must always be maintained in a sorted structure, overlooking the performance trade-off between continuous sorting overhead (TreeMap) and on-demand sorting (HashMap) when insertions are frequent and reads are rare.
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
✓
Use HashMap and sort with Collections.sort when needed
The application is single-threaded and reports are infrequent, so the overhead of sorting a HashMap's entries only when needed is acceptable and avoids the continuous insertion cost of a TreeMap. HashMap provides O(1) insertions, and sorting an ArrayList of 10 million entries with Collections.sort (which uses TimSort, O(n log n)) is efficient for occasional use, making this the best balance of performance and simplicity.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Use ConcurrentSkipListMap
Why it's wrong here
Overkill for single-threaded; higher overhead than TreeMap.
- ✗
Use LinkedHashMap and convert to sorted list when needed
Why it's wrong here
LinkedHashMap preserves insertion order, not sorted; still need sort.
- ✓
Use HashMap and sort with Collections.sort when needed
Why this is correct
Optimal balance: fast inserts, sorting only on demand.
- ✗
Revert to TreeMap because it always keeps data sorted
Why it's wrong here
TreeMap insertions are O(log n) each, slower for high volume.
About these practice questions
Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →
Last reviewed: Jun 25, 2026
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.
Question Discussion
Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.
Sign in to join the discussion.