HashSet, TreeSet, LinkedHashSet
The Set interface data type storage method in the Java programming language is a built-in feature of the Java language framework. The Set data type in Java helps Java users represent collections of multiple elements. Furthermore, the Set data type cannot contain duplicate elements or values. The Set interface in Java programs contains three commonly used basic set concepts: HashSet, TreeSet, and LinkedHashSet.

Three Set interface elements in the popular Java language.
- HashSet
- TreeSet
- LinkedHashSet
Each Set data type element used in Java has its own unique functions or features for its own implementation, where these Set data types can have various impacts and applications.
So, let’s take a closer look at the HashSet, TreeSet, and LinkedHashSet in Java programming.
HashSet data type in Java.
The HashSet data type in Java programs is a useful implementation of the Set interface data type, which uses or follows a hash table (in reality, a HashMap) for secondary storage in Java programs. The Java HashSet data type does not indicate any particular sequence or order of stored data elements.
Special features of the HashSet set data type.
- No duplicates – Declaring a HashSet in a Java program automatically removes duplicate element values from the set. If a Java user tries to add a data element value to a data element already stored in the declared set, that element will not be added to the existing set data type.
- Unordered – The data elements stored in a HashSet data type in Java programs are unordered in nature. Here, the stored HashSet data type does not guarantee the order in which the stored HashSet elements will be stored or extracted. HashSet provides fast program execution with time complexity (O(1)) for applying basic HashSet operations, such as adding elements, removing elements, and checking membership.
- Not synchronized – The HashSet set data type used in Java is not thread-safe by default. If Java users require thread-safe features, they can synchronize it externally or use the Collections.synchronizedSet() function method.
HashSet is a set data type example.
import java.util.HashSet;
public class HashSetDatatype {
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
// Here we are adding elements to a hashset in Java
set.add(“India”);
set.add(“USA”);
set.add(“Russia”);
set.add(“China”);
set.add(“Russia”); // here it remove Duplicate HashSet element, and this will not be added
// here it Iterating through the HashSet element
for (String country : set) {
System.out.println(country); // here the hashset Output will be in random order
}
// here it Checking for existence of hashset element
if (set.contains(“India”)) {
System.out.println(“here India is in the set element”);
}
// here we remove existing hashset element
set.remove(“China”);
System.out.println(“After remove China hashset element – ” + set);
}
}
TreeSet data type in Java.
A TreeSet data type in a Java program provides its user with a set implementation concept interface that follows the Red-Black Tree concept for secondary storage in the TreeSet data type in Java programs. The TreeSet data type stores user-declared element values according to their natural storage order or in a sorted order by a Comparator provided at the time of their creation.
Special features of the TreeSet data type.
- Sorted – The TreeSet data type holds the storage element order of an element in a Java program. Elements stored in the TreeSet data type are stored or displayed in ascending order by default.
- No Duplicates – The TreeSet data type, unlike other Set data types, does not provide the Java user with duplicate value storage permissions. Here TreeSet data type has logarithmic time complexity – The time complexity of TreeSet operations like adding elements, removing elements and checking is defined as O(log n) due to its internal tree structure.
- Not synchronized – Like HashSet data type in Java programs, TreeSet data type also does not allow synchronized features.
Example of TreeSet data type.
import java.util.TreeSet;
public class TreeSetDataType {
public static void main(String[] args) {
TreeSet<String> set = new TreeSet<>();
// here we Adding a multiple TreeSet data type elements
set.add(“India”);
set.add(“USA”);
set.add(“Russia”);
set.add(“China”);
set.add(“Russia”); // here it will remove Duplicate TreeSet element, this will not be added
// here it Iterating through the TreeSet (sorted order manner)
for (String country : set) {
System.out.println(country);
}
// here it Checking for existence of TreeSet element
if (set.contains(“India”)) {
System.out.println(“here India is in the set element”);
}
// Here we are removing a TreeSet element
set.remove(“China”);
System.out.println(“After TreeSet element removal – ” + set);
}
}
LinkedHashSet data type in Java.
In Java programming, LinkedHashSet helps implement the Set database interface. Java LinkedHashSet uses or follows a combination of hash tables and linked lists. Additionally, LinkedHashSets maintain the insertion order of user data elements. This means that LinkedHashSet data elements are stored and processed in the same order in which they were added by the Java user.
Special features of the LinkedHashSet data type.
- Insertion order – Unlike the Java HashSet set data type, which does not store any data element ordering, a user-declared LinkedHashSet in Java maintains the order in which data elements were inserted or added.
- No duplicates – Like other data sets, the LinkedHashSet set data type does not allow duplicate values or content.
- Fast operation – In Java programs, the LinkedHashSet data type provides constant-time performance (O(1)) for adding, removing, and other containment operations, similar to HashSet. However, LinkedHashSet incurs additional overhead to maintain the data insertion order.
- Not synchronized – Like other sets, the LinkedHashSet data type is not synchronized in Java programs.
Example of the LinkedHashSet data type.
import java.util.LinkedHashSet;
public class LinkedHashDataType {
public static void main(String[] args) {
LinkedHashSet<String> set = new LinkedHashSet<>();
// here we Adding a LinkedHashSet data elements
set.add(“India”);
set.add(“USA”);
set.add(“Russia”);
set.add(“China”);
set.add(“Russia”); // here we declare a Duplicate LinkedHashSet element, that will not be added
// here it Iterating through the LinkedHashSet (insertion order manner)
for (String country : set) {
System.out.println(country); // Output: India, Russia, China (insertion order)
}
// Here it is Checking for the existence of a LinkedHashSet element
if (set.contains(“India”)) {
System.out.println(“India is in the set”);
}
// Here it is Removing a LinkedHashSet element
set.remove(“China”);
System.out.println(“here After removal LinkedHashSet element – ” + set);
}
}
Main difference between HashSet, TreeSet, and LinkedHashSet.
| Each set Feature | HashSet data type | TreeSet data type | LinkedHashSet data type |
| Storage Ordering | HashSet data typeNo ordering allowed, it means it store set data in (unordered) nature | TreeSet data type store data element in Sorted order (ascending, natural order or comparator) for each element | LinkedHashSet data type keep Maintains its data type insertion order |
| Duplicates nature | HashSet data typeDoes not allow duplicates set element value | Even TreeSet data type Does not allow any set duplicates values | LinkedHashSet data type Does not allow any duplicates set values |
| Underlying Structure behaviour | HashSet data typeuseHash table | TreeSet data type Red-Black Tree (Self-balancing binary search tree) structure | It uses Hash table with linked list structure |
| Time Complexity set data (Add, Remove, Contains) operation | HashSet data use O(1) (average case) while apply operation | TreeSet data type use O(log n) during apply operation | LinkedHashSet data type O(1) (average case) for data operation |
| Thread Safety features | HashSet data type Not synchronized | TreeSet data type Not synchronized | LinkedHashSet data type Not synchronized |
| Where to Use Case | HashSet data type use in Fast lookup, insert, and delete without concern for data element order | TreeSet data type use where you Need for sorted data elements (natural order or custom comparator) according to need | LinkedHashSet data type used When order of data element insertion is matters |
HashSet, TreeSet, and LinkedHashSet Java Summary.
- HashSet – Apply the HashSet data type in a Java program when the Java user does not need the storage order sequence of data elements and needs the best performance for adding, removing, and checking the existence of a HashSet element, such as for fast lookup operations, etc.
- TreeSet – Apply the TreeSet data type in a Java program when When a Java user needs to store TreeSet data elements in a specific sequence, and a Java programmer needs fast logarithmic time complexity for adding TreeSet elements, removing existing TreeSet elements, and querying the TreeSet.
- LinkedHashSet – Apply LinkedHashSet data type in Java programming when a Java user needs to store LinkedHashSet data elements in the same sequence order in which they were inserted, along with proper ordering of LinkedHashSet element add, remove, and contain operations.
