For working professionals
For fresh graduates
More
6. JDK in Java
7. C++ Vs Java
16. Java If-else
18. Loops in Java
20. For Loop in Java
45. Packages in Java
52. Java Collection
55. Generics In Java
56. Java Interfaces
59. Streams in Java
62. Thread in Java
66. Deadlock in Java
73. Applet in Java
74. Java Swing
75. Java Frameworks
77. JUnit Testing
80. Jar file in Java
81. Java Clean Code
85. Java 8 features
86. String in Java
92. HashMap in Java
97. Enum in Java
100. Hashcode in Java
104. Linked List in Java
108. Array Length in Java
110. Split in java
111. Map In Java
114. HashSet in Java
117. DateFormat in Java
120. Java List Size
121. Java APIs
127. Identifiers in Java
129. Set in Java
131. Try Catch in Java
132. Bubble Sort in Java
134. Queue in Java
141. Jagged Array in Java
143. Java String Format
144. Replace in Java
145. charAt() in Java
146. CompareTo in Java
150. parseInt in Java
152. Abstraction in Java
153. String Input in Java
155. instanceof in Java
156. Math Floor in Java
157. Selection Sort Java
158. int to char in Java
163. Deque in Java
171. Trim in Java
172. RxJava
173. Recursion in Java
174. HashSet Java
176. Square Root in Java
189. Javafx
The HashSet class in Java is part of the Java Collections Framework and stores unique elements, automatically eliminating duplicates. Built on a hash table structure, HashSet offers fast operations like insertion, deletion, and retrieval, making it an efficient choice for handling data in modern applications.
In this tutorial, you'll learn everything you need to know about HashSet—its definition, features, hierarchy, constructors, methods, and practical examples tailored to today's development needs.
Improve your Java programming skills with our Software Development courses — take the next step in your learning journey!
HashSet is a class that implements the Set interface in Java. It stores a collection of elements where each element is guaranteed to be unique. HashSet is backed by a hash table, which helps achieve constant time complexity for basic operations like adding and removing elements.
Here are the key characteristics of HashSet:
Here’s how HashSet differs from other set implementations (TreeSet, LinkedHashSet)
Feature | HashSet | TreeSet | LinkedHashSet |
Ordering | No ordering (unordered) | Sorted (elements ordered by natural order or comparator) | Insertion order (preserves the order of elements as they are added) |
Performance | Fast (constant time for add/remove) | Slower (logarithmic time for add/remove) | Moderate (slower than HashSet but faster than TreeSet) |
Null Elements | Allows one null element | Does not allow null elements | Allows one null element |
Use Case | General use cases with fast operations | When elements need to be sorted | When maintaining the insertion order is important |
Also Read: Exploring the 14 Key Advantages of Java: Why It Remains a Developer's Top Choice in 2025
Now, let’s understand the working of Java HashSet operations.
Now that you understand the basic features of HashSet, let's dive into how you can work with it in Java. You'll learn how to create a HashSet, add and remove elements, check for the existence of elements, and iterate through a HashSet.
To create a HashSet in Java, you can simply use its constructor. By default, a HashSet is created without any elements, but you can also initialize it with a collection of elements.
Example: Creating an Empty HashSet
import java.util.HashSet;
public class CreateHashSetExample {
public static void main(String[] args) {
// Creating an empty HashSet
HashSet<String> set = new HashSet<>();
System.out.println("HashSet created: " + set);
}
}
Explanation:
Output:
HashSet created: []
Adding elements to a HashSet is done using the add() method. It returns true if the element was successfully added and false if it already exists in the set.
Example: Adding Elements
import java.util.HashSet;
public class AddElementsExample {
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
// Adding elements to the HashSet
set.add("Apple");
set.add("Banana");
set.add("Cherry");
set.add("Apple"); // Duplicate element, will not be added
System.out.println("HashSet after adding elements: " + set);
}
}
Explanation:
Output:
HashSet after adding elements: [Banana, Apple, Cherry]
Removing Elements from HashSet
To remove elements from a HashSet, you use the remove() method, which returns true if the element was successfully removed.
Example: Removing Elements
import java.util.HashSet;
public class RemoveElementsExample {
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
set.add("Apple");
set.add("Banana");
set.add("Cherry");
// Removing an element from the HashSet
boolean removed = set.remove("Banana");
System.out.println("Was 'Banana' removed? " + removed);
System.out.println("HashSet after removal: " + set);
}
}
Explanation:
Output:
Was 'Banana' removed? true
HashSet after removal: [Apple, Cherry]
To check if an element exists in the HashSet, you use the contains() method. It returns true if the set contains the specified element.
Example: Checking for Existence
import java.util.HashSet;
public class ContainsExample {
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
set.add("Apple");
set.add("Banana");
set.add("Cherry");
// Checking if an element exists in the HashSet
boolean hasApple = set.contains("Apple");
boolean hasOrange = set.contains("Orange");
System.out.println("Does the set contain 'Apple'? " + hasApple);
System.out.println("Does the set contain 'Orange'? " + hasOrange);
}
}
Explanation:
Output:
Does the set contain 'Apple'? true
Does the set contain 'Orange'? false
To iterate through the elements of a HashSet, you can use a for-each loop or an Iterator. Since HashSet is unordered, the iteration order may not match the order in which the elements were added.
Example: Iterating Through a HashSet
import java.util.HashSet;
public class IterateHashSetExample {
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
set.add("Apple");
set.add("Banana");
set.add("Cherry");
// Iterating through the HashSet using for-each loop
System.out.println("Iterating through HashSet:");
for (String fruit : set) {
System.out.println(fruit);
}
}
}
Explanation:
Output:
Iterating through HashSet:
Banana
Apple
Cherry
Note: The order of output might vary due to the unordered nature of HashSet.
Also Read: Differences Between HashMap and HashTable in Java
These fundamental operations allow you to effectively work with a HashSet in Java. Now, let’s look at performance considerations and advanced usage of Java HashSet operations.
When working with HashSet in Java, understanding its performance and internal workings is crucial for optimizing operations in your applications. Let's dive into the time complexity of common Java HashSet operations, its internal structure, and when it is the best choice to use HashSet.
The performance of HashSet operations is primarily determined by the underlying hash table structure. Most operations—adding, removing, and checking for existence of elements—are typically very fast and have constant time complexity, O(1), on average.
Operation | Time Complexity |
Add (add()) | O(1) |
Remove (remove()) | O(1) |
Contains (contains()) | O(1) |
Iteration (iterator) | O(n) |
However, in the worst case (when there are too many hash collisions), the time complexity could degrade to O(n). But this situation is rare, especially if the hash function is well-distributed.
HashSet uses a HashMap internally to store its elements as keys. The reason for this is that HashSet needs to store unique elements, and HashMap allows for efficient storage and lookup of keys (which, in the case of a HashSet, are the elements themselves).
Here's how it works internally:
Example: HashSet Behind the Scenes
import java.util.HashSet;
public class HashSetInternalWorking {
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
set.add("Apple");
set.add("Banana");
set.add("Cherry");
// Internally, HashSet uses HashMap for storage
// HashSet stores elements as keys of the HashMap, and values are irrelevant
System.out.println("HashSet contents: " + set);
}
}
Explanation:
HashSet contents: [Apple, Banana, Cherry]
Each string is used as a key in a HashMap, and its corresponding value is irrelevant (not used in HashSet). This allows O(1) average time complexity for adding, removing, and checking for the existence of elements.
While HashSet is efficient for most use cases, it's important to understand when it is the best choice for storing data.
1. When You Need Unique Elements: If you need to store a collection where duplicates are not allowed, HashSet is ideal. It automatically prevents duplicate entries.
2. When Order Does Not Matter: HashSet does not guarantee any specific order for its elements. If the order in which elements were added is not important, HashSet is a great choice for efficiency.
3. Fast Operations: If you need fast insertion, deletion, and lookups, HashSet provides O(1) average time complexity for these operations, making it highly efficient compared to other set implementations like TreeSet or LinkedHashSet, which have slower performance.
4. Handling Large Datasets: For large datasets where speed is a concern, HashSet provides an efficient way to check for membership and manage large collections of unique elements. This is particularly useful in scenarios such as caching, deduplication, or set operations (e.g., union, intersection).
Also Read: Careers in Java: How to Make a Successful Career in Java in 2025
To solidify your understanding of Java HashSet operations, test your knowledge with a quiz. It’ll help reinforce the concepts discussed throughout the tutorial and ensure you're ready to apply them in your projects.
Assess your understanding of HashSet operations, performance, and best practices in Java by answering the following multiple-choice questions. Dive in!
1. Which of the following is a characteristic of HashSet in Java?
a) It allows duplicate elements.
b) It maintains the insertion order.
c) It allows null elements.
d) It is backed by a List.
2. How does HashSet handle duplicates when you try to add an element?
a) It throws an exception.
b) It adds the duplicate element and prints a warning.
c) It simply ignores the duplicate element.
d) It updates the existing element with the new value.
3. What is the time complexity of the add() method in a HashSet in the average case?
a) O(n)
b) O(log n)
c) O(1)
d) O(n log n)
4. Which of the following is true about HashSet compared to TreeSet?
a) HashSet stores elements in a sorted order, while TreeSet does not.
b) HashSet provides faster performance for large datasets.
c) TreeSet allows duplicate elements, while HashSet does not.
d) TreeSet is unordered, while HashSet maintains the insertion order.
5. Which method is used to remove an element from a HashSet?
a) remove()
b) delete()
c) discard()
d) erase()
6. What does the contains() method in HashSet return?
a) It returns false if the element is not found.
b) It returns true if the element is found, and false otherwise.
c) It throws an exception if the element is not found.
d) It returns null if the element is found.
7. What is the internal data structure used by HashSet to store elements?
a) ArrayList
b) TreeMap
c) HashMap
d) LinkedList
8. How can you iterate through the elements of a HashSet?
a) Using a for-each loop or an Iterator.
b) Using a while loop only.
c) Using a for loop with an index.
d) You cannot iterate through a HashSet.
9. When is it best to use a HashSet in Java?
a) When you need to store elements in a sorted order.
b) When you need to preserve the insertion order of elements.
c) When you need fast lookup and unique elements.
d) When you want to store elements with duplicates.
10. Which of the following statements is true about HashSet in Java?
a) HashSet allows duplicate elements but only in different positions.
b) The order of elements in HashSet is predictable and follows insertion order.
c) HashSet uses a hash table for storage, which allows for fast operations.
d) HashSet guarantees that the order of elements will always be ascending.
This quiz ensures you have a solid understanding of HashSet in Java, its features, operations, and when to use it effectively in your applications.
Also Read: Top 8 Reasons Why Java Is So Popular and Widely Used in 2025
You can continue expanding your skills in Java with upGrad, which will help you deepen your understanding of advanced Java concepts and real-world applications.
upGrad’s courses offer expert training in Java programming, with a focus on Java HashSet operations, best practices, and performance optimization. Gain hands-on experience in working with collections, efficiently managing unique elements, and building high-performance Java applications.
Below are some relevant upGrad courses:
You can also get personalized career counseling with upGrad to guide your career path, or visit your nearest upGrad center and start hands-on training today!
Similar Reads:
Q: Can a HashSet in Java store multiple null values?
A: No, a HashSet can only store one null value. If you try to add more than one null, only the first null will be stored.
Q: Why does HashSet not preserve the order of elements?
A: The elements in HashSet are stored based on their hash codes and do not maintain any specific order, unlike LinkedHashSet or TreeSet, which preserve insertion order or sorting, respectively.
Q: How do you ensure HashSet performs optimally when adding a large number of elements?
A: To optimize HashSet performance, it’s a good practice to initialize it with an appropriate initial capacity and load factor, which reduces resizing operations during frequent additions.
Q: What happens if you modify an element in HashSet after adding it?
A: HashSet uses the hash code of elements to store them. If you modify an element (e.g., changing its field that affects its hash code), it may not be correctly located within the set, leading to unexpected behavior.
Q: Is HashSet thread-safe? Can it be used in a multi-threaded environment?
A: By default, HashSet is not thread-safe. To make it thread-safe, you can either use Collections.synchronizedSet(new HashSet<>) or use a concurrent collection like CopyOnWriteArraySet for specific use cases.
Q: How can you check if HashSet contains any common elements with another collection?
A: You can use the retainAll() method to retain only the elements that are common between two collections. It modifies the HashSet to include only the common elements.
Q: What is the effect of increasing the load factor in HashSet?
A: A higher load factor increases the chance of fewer rehashing operations, but it also increases the time complexity for insertion and retrieval. A load factor of 0.75 is typically recommended as a balance between space and performance.
Q: What happens when two elements have the same hash code in a HashSet?
A: When two elements have the same hash code, HashSet uses separate chaining (linked lists or trees) to store the elements in the same bucket, avoiding collision but still ensuring uniqueness.
Q: Can you use custom objects as elements in HashSet? If so, how?
A: Yes, you can store custom objects in a HashSet, but you need to override the equals() and hashCode() methods in your class to ensure proper handling of equality and hash-based storage.
Q: How does HashSet handle large-scale data, and what makes it a good choice for such use cases?
A: HashSet is ideal for large datasets because of its O(1) average time complexity for add(), remove(), and contains() operations, making it faster than other collections like ArrayList for checking membership or removing elements.
Q: How can I convert a HashSet back into a List or an Array?
A: You can easily convert a HashSet to a List using the ArrayList constructor:
List<String> list = new ArrayList<>(hashSet);
For converting to an Array, use the toArray() method:
String[] array = hashSet.toArray(new String[0]);
Both methods help you transform the HashSet into a different collection type.
Take the Free Quiz on Java
Answer quick questions and assess your Java knowledge
Author
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.