Learn Java
import com.learnjava.datastructurescollections.HashsetTreeset;

HashSet & TreeSet in Java

@Concept

Both HashSet and TreeSet, from java.util, store a collection of unique elements — adding a duplicate simply has no effect, since a Set can never contain the same element twice. They differ in ordering and performance: HashSet offers no guaranteed order at all but constant-time add, remove, and contains checks on average, while TreeSet keeps its elements sorted at all times, at the cost of logarithmic-time operations instead.

TreeSet's sorting relies on either the elements' natural ordering (via Comparable, which built-in types like String and Integer already implement) or an explicit Comparator supplied when the TreeSet is created. Uniqueness itself is determined by equals() for HashSet, or by that same ordering comparison for TreeSet.

@HowItWorks
1

Sets store unique elements

Adding a value already present has no effect; a Set never holds duplicates.

2

HashSet

Offers no guaranteed iteration order, but constant-time add, remove, and contains on average.

3

TreeSet

Keeps elements sorted at all times, using either their natural ordering or a supplied Comparator, at logarithmic-time cost per operation.

4

Uniqueness is determined differently

HashSet uses equals() and hashCode() to detect duplicates; TreeSet uses its ordering comparison instead.

5

Choosing between them

Reach for HashSet when order doesn't matter and speed does; reach for TreeSet when elements need to stay sorted automatically.

@Example
import java.util.HashSet;
import java.util.TreeSet;
 
public class SetDemo {
public static void main(String[] args) {
HashSet<Integer> hashSet = new HashSet<>();
hashSet.add(3);
hashSet.add(1);
hashSet.add(3);
 
TreeSet<Integer> treeSet = new TreeSet<>(hashSet);
System.out.println("HashSet size: " + hashSet.size());
System.out.println("Sorted: " + treeSet);
}
}
Output
HashSet size: 2
Sorted: [1, 3]
1Adding 3 twice has no effect the second time, since a Set can't hold duplicates, so hashSet ends up with just two elements.
2hashSet.size() confirms 2, not 3.
3Building a TreeSet from hashSet copies those same elements in but keeps them sorted from that point on.
4Printing treeSet shows 1 before 3, its natural numeric order, regardless of the order elements were originally added.
@Complexity
time HashSet: O(1) average for add/remove/contains; TreeSet: O(log n) for the same operations
space O(n)

HashSet trades away any ordering guarantee for speed; TreeSet keeps a sorted structure at all times, which is what its logarithmic cost per operation pays for.

@UseCases
Removing duplicates from a collection of values automatically
Checking whether a value has already been seen, quickly, with HashSet
Maintaining a collection that always stays sorted, like a leaderboard, with TreeSet
Finding the smallest or largest remaining element quickly with TreeSet's first() and last()

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.