1Z0-829 Working with Arrays and Collections Practice Question
Which statement about TreeSet is true when using a custom Comparator that does not define equals() consistently with compare()?
⚠ Common exam trap
Watch out — candidates often assume TreeSet uses equals() for duplicate detection because of the general Set contract, but TreeSet (and TreeMap) specifically override that behavior to use compare()/compareTo() instead.
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
✓
TreeSet considers two elements equal if the comparator returns 0.
TreeSet uses the compare() method of the provided Comparator (or the natural ordering's compareTo()) to determine element equality. When a custom Comparator returns 0 for two elements, TreeSet treats them as duplicates and does not add the second element, regardless of what equals() returns. This is specified in the Java Collections Framework documentation and is critical for maintaining the Set contract.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
TreeSet uses equals() to determine duplicates.
Why it's wrong here
Uses compare/compareTo.
- ✗
TreeSet uses hashCode() for ordering.
Why it's wrong here
Ordering is based on compare/compareTo.
- ✓
TreeSet considers two elements equal if the comparator returns 0.
Why this is correct
Set uses comparator's compare method.
- ✗
TreeSet throws IllegalArgumentException if equals() is inconsistent.
Why it's wrong here
No exception; but behavior may violate Set contract.
Go deeper
Related to this question
About these practice questions
Courseiva writes every 1Z0-829 question from scratch — 513 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or 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.