Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📘 Lesson  ·  Lesson 41

HashSet vs TreeSet in Java

In this lesson: HashSet vs TreeSet compared — ordering, speed, null handling — with examples and comparison table. In Hindi & English.

Two kinds of Set

HashSet and TreeSet are both Sets — they store only unique items — but the big difference is order. HashSet keeps no order and is faster; TreeSet keeps items automatically sorted but is a little slower. You pick based on whether you need sorting.

💡 HashSet = unique, no order, fast. TreeSet = unique, always sorted, a bit slower.

HashSet

import java.util.HashSet;

HashSet<Integer> set = new HashSet<>();
set.add(30); set.add(10); set.add(20);
set.add(10);            // duplicate ignored
System.out.println(set);   // order not guaranteed
[20, 10, 30]

HashSet stores unique items with no guaranteed order, which makes it very fast for add, remove and contains. Use it when you only care about uniqueness and speed.

TreeSet

import java.util.TreeSet;

TreeSet<Integer> set = new TreeSet<>();
set.add(30); set.add(10); set.add(20);
System.out.println(set);   // sorted automatically
[10, 20, 30]

TreeSet keeps items in sorted order automatically. Adding 30, 10, 20 gives them back as 10, 20, 30. It is the choice when you need a set that stays sorted.

Order is the big difference

  • HashSet — no order; items may appear in any arrangement.
  • TreeSet — natural sorted order (numbers ascending, strings A-Z).
  • LinkedHashSet — a third option that keeps insertion order.

If order does not matter, HashSet is best. If you need sorting, use TreeSet.

Performance and nulls

  • Speed: HashSet is faster (near constant time); TreeSet is slower (keeps sorting).
  • Null: HashSet allows one null; TreeSet allows none (it must compare to sort).
  • Requirement: TreeSet elements must be comparable.

Comparison

PointHashSetTreeSet
Unique itemsYesYes
OrderNoneSorted
SpeedFasterSlower
Null allowedOneNone
Best forSpeed, uniquenessSorted unique data

Common mistakes

  • Expecting order from HashSet. It has none — use TreeSet or LinkedHashSet.
  • Adding null to TreeSet. It throws NullPointerException.
  • Using TreeSet when order is not needed. HashSet is faster.
  • Non-comparable elements in TreeSet. They must be comparable, or supply a Comparator.

Summary

  • Both store only unique items.
  • HashSet has no order and is faster.
  • TreeSet keeps items sorted but is slower.
  • HashSet allows one null; TreeSet allows none.
  • Use HashSet for speed, TreeSet for sorted data, LinkedHashSet for insertion order.

Next, revise sets in the Collections Framework, or learn sorting rules with Comparable vs Comparator.

Frequently Asked Questions

What is the difference between HashSet and TreeSet?
Both store only unique items, but HashSet does not keep any order and is faster, while TreeSet keeps items sorted automatically and is a bit slower. Choose HashSet for speed, TreeSet when you need sorted order.
Does HashSet keep insertion order?
No. HashSet stores items in an unpredictable order for speed. If you need insertion order, use LinkedHashSet; if you need sorted order, use TreeSet.
How does TreeSet sort its items?
TreeSet keeps items in natural sorted order (numbers ascending, strings alphabetically) using their compareTo method, or a Comparator you provide. Every element must be comparable, or it throws an error.
Which is faster, HashSet or TreeSet?
HashSet is faster for add, remove and contains (roughly constant time). TreeSet is slower because it keeps everything sorted. Use HashSet unless you specifically need ordering.
Can HashSet and TreeSet store null?
HashSet allows a single null element. TreeSet does not allow null because it must compare elements to sort them, and comparing null causes a NullPointerException.
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicComparable vs Comparator Your progress: 41 / 61 (67%) · Lesson 42 / 61 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free