TreeSet and HashSet are both implementations of the
Set interface in Java, but they have significant differences in terms of their internal workings, performance, and behavior.
TreeSet
TreeSet is a class in Java that implements the Set interface and is part of the Java Collections Framework. It uses a red-black tree internally to store elements in a sorted order. The elements in a
TreeSet are sorted either according to their natural ordering or according to a
Comparator provided at set creation time.
Key Characteristics:
Maintains elements in ascending order.
Provides guaranteed log(n) time cost for the basic operations (add,
remove, and contains).
Does not allow null elements.
Implements the NavigableSet interface, providing methods for navigating the set.
Useful for scenarios where sorted order and fast range operations are required.
Example:
import java.util.Set;
import java.util.TreeSet;
public class TreeSetExample {
public static void main(String[] args) {
Set<String> treeSet = new TreeSet<>();
treeSet.add("Apple");
treeSet.add("Banana");
treeSet.add("Orange");
// Elements are in sorted order
for (String element : treeSet) {
System.out.println(element);
}
}
}
Output:
Apple
Banana
Orange
HashSet
HashSet is a class in Java that implements the Set interface and is part of the Java Collections Framework. It uses a hash table (internally backed by a
HashMap) to store elements, providing constant-time performance for basic operations (add,
remove, and contains), assuming the hash function disperses the elements properly among the buckets.
Key Characteristics:
Does not maintain any order of elements.
Allows one null element.
Provides O(1) time complexity for basic operations in the average case.
Not synchronized, but can be synchronized externally if needed.
Suitable for applications that require a fast, unordered collection of unique elements.
Example:
import java.util.HashSet;
import java.util.Set;
public class HashSetExample {
public static void main(String[] args) {
Set<String> hashSet = new HashSet<>();
hashSet.add("Apple");
hashSet.add("Banana");
hashSet.add("Orange");
// Elements are not in a specific order
for (String element : hashSet) {
System.out.println(element);
}
}
}
Output:
Apple
Orange
Banana
Note
TreeSet: A sorted set backed by a red-black tree, providing log(n) time complexity for basic operations and maintaining elements in ascending order.
HashSet: An unordered set backed by a hash table, providing constant-time performance for basic operations and not maintaining any specific order.
Summary of Differences
Feature
HashSet
TreeSet
Internal Structure
Hash table
Red-black tree
Ordering
No order
Sorted order
Performance
O(1) for basic operations
O(log n) for basic operations
Null Handling
Allows one null element
Does not allow null elements
Use Cases
Fast access, insertion, deletion
Sorted elements, range operations
The choice between HashSet and TreeSet depends on the specific requirements of your application. If you need a fast, unordered collection,
HashSet is appropriate. If you need a sorted collection and can tolerate slightly slower performance,
TreeSet is the better option.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
TreeSetandHashSetare both implementations of theSetinterface in Java, but they have significant differences in terms of their internal workings, performance, and behavior.TreeSet
TreeSetis a class in Java that implements theSetinterface and is part of the Java Collections Framework. It uses a red-black tree internally to store elements in a sorted order. The elements in aTreeSetare sorted either according to their natural ordering or according to aComparatorprovided at set creation time.Key Characteristics:
add,remove, andcontains).NavigableSetinterface, providing methods for navigating the set.Example:
Output:
HashSet
HashSetis a class in Java that implements theSetinterface and is part of the Java Collections Framework. It uses a hash table (internally backed by aHashMap) to store elements, providing constant-time performance for basic operations (add,remove, andcontains), assuming the hash function disperses the elements properly among the buckets.Key Characteristics:
Example:
Output:
Note
Summary of Differences
The choice between
HashSetandTreeSetdepends on the specific requirements of your application. If you need a fast, unordered collection,HashSetis appropriate. If you need a sorted collection and can tolerate slightly slower performance,TreeSetis the better option.Read more
How to get the highest (last) element in trees when trees are sorted with another attribute
What is the difference between HashSet and TreeSet?
What are the Similarities between ArrayList and HashSet?