In this post, we will see the difference between HashSet and TreeSet in Java. First, we will see HashSet vs TreeSet in brief later we will see each point in details.
| HashSet | TreeSet |
|---|---|
| 1. HashSet does not maintain any order. | 1. TreeSet maintains ascending order.. |
| 2. Hashset allows null value. | 2. In case of TreeSet if you add null value it will not give any compile time error but when you will try to iterate the elements it will throw NullPointerException. |
| 3. Hashset is faster than TreeSet. | 3. TreeSet is slower than HashSet because it internally perform sorting operation. |
| 4. Hashset internally uses HashMap | 4. TreeSet internally uses NavigableMap. |
| 5. If we define generic type Object, in case of HashSet we can have different type of element. | 5. In case of TreeSet , we can't have different type of elements. |
Let’s see all points in detail.
HashSet does not maintain any order whereas Treeset maintains ascending order.
Example of HashSet –
import java.util.HashSet;
import java.util.Set;
public class HashSetExample {
public static void main(String[] args) {
Set<String> setObject = new HashSet<>();
setObject.add("ram");
setObject.add("mohan");
setObject.add("shyam");
setObject.add("mohan");
// duplicates are not allowed but it will not give
// any compilation error
setObject.add("ram");
System.out.println("set object -- " + setObject);
}
}
Output is –
set object — [shyam, mohan, ram]
Example of TreeSet –
package treeset;
import java.util.Set;
import java.util.TreeSet;
public class TreeSetExample {
public static void main(String[] args) {
Set<String> treeSetObj = new TreeSet<>();
treeSetObj.add("ram");
treeSetObj.add("mohan");
treeSetObj.add("sohan");
treeSetObj.add("rohan");
treeSetObj.add("ram");
System.out.println(treeSetObj);
}
}
Output is –
[mohan, ram, rohan, sohan]
In the above example, we can see TreeSet is maintaining ascending order whereas HashSet is unordered.
HashSet allows null value, TreeSet will throw NullPointerException.
package hashsetvstreeset;
import java.util.*;
public class HashSetVsTreeSetGet {
public static void main(String[] args) {
Set<String> setObject = new HashSet<>();
setObject.add("ram");
setObject.add("mohan");
setObject.add("shyam");
setObject.add("mohan");
//we can have null in HashSet
setObject.add(null);
for (String str : setObject) {
System.out.println(str);
}
Set<String> treeSetObj = new TreeSet<>();
treeSetObj.add("ram");
treeSetObj.add("mohan");
treeSetObj.add("sohan");
treeSetObj.add("rohan");
//TreeSet doen't allow null.
//At Compile time there is no problem
//While running it will throw NullPointerException
treeSetObj.add(null);
for (String str : treeSetObj) {
System.out.println(str);
}
}
}
Output is –
null
shyam
mohan
ram
Exception in thread “main” java.lang.NullPointerException
at java.util.TreeMap.put(Unknown Source)
at java.util.TreeSet.add(Unknown Source)
at hashsetvstreesetperformance.HashSetVsTreeSetGet.main(HashSetVsTreeSetGet.java:29)
HashSet internally uses HashMap whereas TreeSet internally uses NavigableMap.
public class HashSet<E>
extends AbstractSet<E>
implements Set<E>, Cloneable, java.io.Serializable
{
static final long serialVersionUID = -5024744406713321676L;
private transient HashMap<E,Object> map;
private static final Object PRESENT = new Object();
public HashSet() {
map = new HashMap<>();
}
}
public class TreeSet<E> extends AbstractSet<E>
implements NavigableSet<E>, Cloneable, java.io.Serializable
{
private transient NavigableMap<E,Object> m;
private static final Object PRESENT = new Object();
TreeSet(NavigableMap<E,Object> m) {
this.m = m;
}
}
HashSet we can have different type of elements, if we try to add a different type of elements in TreeSet will throw ClassCastException. TreeSet internally compares two elements, if we have different types of elements it will not able to compare.
package addingtime;
import java.util.*;
public class Example2 {
public static void main(String[] args) {
Set<Object> setObject = new HashSet<>();
setObject.add("ram");
setObject.add("mohan");
setObject.add("shyam");
setObject.add(3);
setObject.add(3.97f);
System.out.println("HashSet can have different types of elements-- " + setObject);
Set<Object> treeSetObj = new TreeSet<>();
treeSetObj.add("ram");
treeSetObj.add("mohan");
treeSetObj.add("sohan");
treeSetObj.add(3);
treeSetObj.add(3.97f);
System.out.println(treeSetObj);
}
}
Output is –
HashSet can have different types of elements– [3, shyam, 3.97, mohan, ram]
Exception in thread “main” java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at java.lang.Integer.compareTo(Unknown Source)
at java.util.TreeMap.put(Unknown Source)
at java.util.TreeSet.add(Unknown Source)
at addingtime.Example2.main(Example2.java:20)
You may like.
- Difference between Iterator and ListIterator in Java.
- Difference between Iteration and Enumeration in java.
- Difference between HashSet and HashMap in Java.
- Difference between List and Set in Java.
- Difference between ArrayList and LinkedList in java.
- ArrayList vs Vector in java.
- How to make List, Set and Map Read Only in Java.
- How to avoid duplicate elements in ArrayList.
- Adding Custom type class object in ArrayList.
- How to Synchronize ArrayList in Java.
- Difference between Arrays and Collections.
- Difference between String and StringBuffer in java.
- Sort HashMap by value in java.
- Sort HashMap by key in java.
- How HashMap works internally in Java.
- How HashSet works internally in java.
- How ArrayList works internally in Java.
- String compareTo() method in java.
- String Constant Pool In Java with Example.



