Interview Q&A · Java · 7 questions in this topic
Sets: HashSet, LinkedHashSet and TreeSet — Java Interview Q&A
How sets enforce uniqueness, and when to choose HashSet, LinkedHashSet or TreeSet. Part of Collections Framework · 294 questions in the Java bank.
What is a Set?
Answer
A Set is a collection that forbids duplicates, so adding an equal element leaves the set unchanged. Most implementations allow at most one null element. Uniqueness rests on the equals and hashCode contract for hashed sets, or on ordering comparison for sorted sets. A set is unordered unless it is a sorted set such as TreeSet or an insertion-ordered set such as LinkedHashSet.
What are the Set implementations?
Answer
The core general-purpose implementations are HashSet, LinkedHashSet, and TreeSet. HashSet gives fast hashed lookup with no order, LinkedHashSet adds predictable insertion order, and TreeSet keeps elements sorted in a Red-Black tree. Modern Java adds EnumSet for enum types, which is extremely compact and fast, and CopyOnWriteArraySet for concurrent read-heavy sets with snapshot iteration. Choose the implementation from whether you need speed, order, sorting, or thread safety.
HashSet features?
Answer
HashSet is a hash-table implementation of Set backed by a HashMap internally. It offers constant-time average performance for add, remove, and contains when elements spread well across buckets. It makes no ordering guarantee and permits at most one null element. Elements must implement sound hashCode and equals methods, because duplicates and lookups are decided by those two methods.
Example
Set<String> tags = new HashSet<>(); tags.add("java"); tags.add("java"); int n = tags.size();
TreeSet features?
Answer
TreeSet is a sorted set implemented as a Red-Black tree and exposed as a NavigableSet. Elements come out in ascending natural order or in the order of a supplied Comparator. Add, remove, and contains cost logarithmic time, which is slower than hashing but keeps the data ordered. With natural ordering null cannot be added because comparison would fail, so use a Comparator that handles nulls or avoid nulls entirely.
Example
Set<Integer> scores = new TreeSet<>(); scores.add(40); scores.add(10); scores.add(30);
When is HashSet preferred over TreeSet?
Answer
Prefer HashSet when you need fast membership tests and do not care about order. Its hashed storage gives constant-time average lookup versus logarithmic time for TreeSet. It also uses less overhead than a tree when iteration order does not matter. Switch to TreeSet only when sorted traversal or range operations justify the extra cost.
When is TreeSet preferred over HashSet?
Answer
Note: the original source numbers this question 82 even though it sits among the 210s, so it is kept as q82 for traceability. Prefer TreeSet when elements must stay sorted or iteration must follow a deterministic order. Its NavigableSet operations support range queries such as subsets, heads, tails, ceiling, floor, and nearest matches. HashSet is faster for plain lookup, but it cannot provide sorted traversal or ordered ranges.
What is LinkedHashSet?
Answer
LinkedHashSet is a HashSet subclass that links entries in a doubly linked list recording insertion order. Iteration therefore returns elements in the order they were first added, while lookup performance stays near HashSet speed. The trade-off is slightly higher memory use and slightly slower insertion because the links must be maintained. Use it when you want HashSet speed plus predictable iteration.
Example
Set<String> order = new LinkedHashSet<>(); order.add("first"); order.add("second");
Learn this topic
Recommended resourcesAffiliate links · may earn us a commission