Java HashSetLast Updated : 12 Jan 2026 Java HashSet class is used to create a collection that uses a hash table for storage. It inherits the AbstractSet class and implements Set interface. The important points about Java HashSet class are:
Difference Between List and SetA list can contain duplicate elements whereas Set contains unique elements only. Hierarchy of HashSet classThe HashSet class extends AbstractSet class which implements Set interface. The Set interface inherits Collection and Iterable interfaces in hierarchical order. A HashSet in Java is a collection class that implements the Set interface, which means it stores a collection of unique elements. It does not allow duplicate elements. Internally, a HashSet uses a HashMap to store its elements. Each element in the HashSet is stored as a key in the HashMap, with a dummy value associated with it. HashSet uses the hash code of an element to determine its position in the underlying hash table, which allows for constant-time performance for basic operations like add, remove, and contains, assuming a good hash function. ![]() One of the key features of a HashSet is its ability to maintain a collection of elements without any specific order. It means that the order in which elements are added to a HashSet is not preserved when iterating over the elements. If we need to maintain the insertion order, we can use a LinkedHashSet, which is another implementation of the Set interface that maintains the insertion order of its elements. HashSet does not allow null elements, but it can contain a single null value. If we attempt to add a duplicate element to a HashSet, the add method will return false and the HashSet will remain unchanged. HashSet also provides methods for set operations like union, intersection, and difference. However, unlike some other collection classes, HashSet is not synchronized, meaning that it is not thread-safe. If you need a synchronized (thread-safe) version of HashSet, we can use the Collections.synchronizedSet method to wrap your HashSet with a synchronized set. HashSet Class DeclarationLet's see the declaration for java.util.HashSet class. Constructors of Java HashSet Class
Methods of Java HashSet Class
Java HashSet ExampleLet's see a simple example of HashSet. Notice, the elements iterate in an unordered collection. HashSetExample1.java Output: Five One Four Two Three Ignoring Duplicate ElementsIn this example, we see that HashSet does not allows duplicate elements. ExampleCompile and RunOutput:
Ajay
Vijay
Ravi
Removing ElementsHere, we see different ways to remove an element. ExampleCompile and RunOutput: An initial list of elements: [Vijay, Ravi, Arun, Sumit] After invoking remove(object) method: [Vijay, Arun, Sumit] Updated List: [Vijay, Arun, Gaurav, Sumit, Ajay] After invoking removeAll() method: [Vijay, Arun, Sumit] After invoking removeIf() method: [Arun, Sumit] After invoking clear() method: [] Java HashSet from another CollectionHashSetExample4.java Output: Vijay Ravi Gaurav Ajay Java HashSet Example: BookLet's see a HashSet example where we are adding books to set and printing all the books. ExampleCompile and RunOutput: 101 Let us C Yashwant Kanetkar BPB 8 102 Data Communications & Networking Forouzan Mc Graw Hill 4 103 Operating System Galvin Wiley 6 Let's understand about Java HashSet in Java with the help of an example program. Example 1:Filename: HashSetExample.java Output:
HashSet: [Apple, Orange, Banana]
Iterating over the HashSet using Iterator: Apple Orange Banana
HashSet converted to array: Apple Orange Banana
HashSet of custom objects: [Person{name='Alice', age=30}, Person{name='Bob', age=25}]
Next TopicJava LinkedHashSet class |
We request you to subscribe our newsletter for upcoming updates.