Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Set is the child interface of Collection in the Collection Framework. Sets are helpful when we want to represent a collection of objects as one entity, where duplicates are not permitted and insertion order is not preserved. In this lesson, we will learn about HashSets.
HashSet is a Java class used to access random elements. Hash tables use hash codes to access their elements.
A hashcode helps identify an element by its unique identity in a hash table.
Duplicate elements are not permitted in hash sets. A unique hashcode is assigned to each element in a hash set.
HashSets check if an object already exists within the Set by using its hashcode value.
There are various bucket locations with multiple elements; each bucket location corresponds to a hash code value, which is the same for all elements. There may be different hash codes, however.
Some features of HashSet
The data structure used internally is HashTable.
No duplicate values are allowed. In the case of duplicate entries, there will be no compile-time or runtime errors. The add() method will return false.
All objects are inserted based on the hash code of all objects, so the insertion order is not preserved.
The order of insertion is not preserved.
Null insertion is possible.
The interface can be serialized and cloned.
In the case of frequent operations such as Search, HashSet is the best choice.
Performance of a HashSet
HashSets performs best when two parameters are set - the initial capacity and load factor.
In the worst-case scenario, adding an element to a set will have O(n) time complexity. Thus, the suitable HashSet's capacity must be maintained.
Load Factor: A set may need to be resized above a certain fill level according to the load factor. It means whenever our hash set is filled by 70%, the elements are moved to a new hash table of double the size of the original hash table.
Initial Capacity: By reducing the initial capacity, the complexity of the space decreases, but the frequency of rehashing increases, resulting in increased costs. However, the initial memory consumption increases if the initial capacity is high.
The internal working of HashSets is that Map serves as an internal backend for all Set interface classes. HashedSets internally store objects in HashMaps. We use a key-value pair in HashMap, whereas we use a single value in HashSet.
Constructors for HashSet
There are four constructors for HashSet:
HashSet h=new HashSet(); // creates an empty HashSet object with default initial capacity 16 and Fill ratio of 0.75 also known as Load Factor.
HashSet h=new HashSet(int initialCapacity); // creates an empty HashSet with customized initial capacity.
HashSet h=new HashSet(int initialCapacity, float load factor); //creates an empty HashSet with customized initial capacity and the Load Factor.
HashSet h=new HashSet(Collection c); // creates an equivalent HashSet object from the given Collection named c. means interconversion between Collection objects.
Here is a java program for creating a HashSet by using another collection.
import java.util.*;
public class MyClass {
public static void main(String args[]) {
HashSet list = new HashSet < > ();
list.add("Ninja");
list.add("2022");
HashSet h = new HashSet(list);
h.add("1");
h.add("two");
h.add("3");
h.add("four");
System.out.println(h);
}
}
You can also try this code with Online Java Compiler
Here is a simple program to show the properties of HashSet.
import java.util.*;
public class MyClass {
public static void main(String args[]) {
HashSet h = new HashSet();
h.add("1");
h.add("two");
h.add("3");
// storing null
h.add(null);
h.add("5");
//Trying to add duplicate value "5"which will return false
System.out.println(h.add("5"));
System.out.println(h);
}
}
You can also try this code with Online Java Compiler
Difference between List and Set. → The sequence of elements in a collection is maintained in List, whereas Set does not preserve the order of the elements. → The List can have duplicate elements, whereas Set does not allow any duplicate elements.
When should HashSets and HashMaps be used in Java? To maintain the uniqueness of the Collection object, we should prefer HashSet to Hashmap. HashMap is better than HashSet in all other cases because of its superior performance.
How do you determine a good initial capacity? High initial capacity is the best option when few or no iterations. For small entries with many iterations, a low initial capacity is better.
Is HashSet is synchronized? There is no synchronization of the HashSet. If more than one thread modifies the hash set simultaneously, the hash set will become corrupted. External synchronization is required.
Conclusion
In this blog, we have seen what HashSet is in the Collection Framework and also the methods, features, and performance of HashSet.
We hope that this blog has helped you enhance your knowledge about HashSet and if you would like to learn more, check out our articles on the link. Do upvote our blog to help other ninjas grow. Happy Coding!