How to Create a HashSet with a Custom Initial Load Factor in Java? Last Updated : 14 Feb, 2024 Comments Improve Suggest changes Like Article Like Report Java HashSet is a simple data structure provided by the Java Collection Framework that provides efficient storage and enables the storage of unique objects. One of the parameters that affect its performance is the load factor, which determines when the underlying hash table should be updated to accommodate multiple items. In this article, we will learn how to create a HashSet with a Custom Initial Load Factor in Java. Load FactorBefore moving to the implementation, let us understand the concept of load factors. The load value represents the ratio of the number of elements in the HashSet to the size of the hash table. When the number of elements exceeds this ratio, the hash table will be updated to be useful. The Default payload in Java's HashSet is 0.75. This is considered a good balance between space complexity and time complexity.Syntax:HashSet<E> hashSet = new HashSet<E>(int initialCapacity, float loadFactor);Program to create a HashSet with Custom Load Factor in JavaTo create a HashSet with a custom initial value in Java, we use the constructor of the HashSet class. This allows the initial capacity and load factor to be determined. Java // Java program to create a HashSet with a custom initial load factor import java.util.*; public class Main{ public static void main(String args[]){ // custom initial capacity int initialCapacity = 16; // custom load factor float customLoadFactor = 0.75f; // create HashSet with custom initial capacity and load factor HashSet<String> customHashSet = new HashSet<>(initialCapacity, customLoadFactor); // add elements to the HashSet customHashSet.add("Element 1"); customHashSet.add("Element 2"); customHashSet.add("Element 3"); // print the HashSet System.out.println("Custom HashSet: " + customHashSet); } } OutputCustom HashSet: [Element 1, Element 3, Element 2] Explanation of the Above Program:The above Java program creates a HashSet with a custom initial capacity and load factor. Then, it initializes the HashSet with a specific initial capacity and load factor.After that it adds elements to the HashSet.And then it prints the contents of the HashSet. Comment More infoAdvertise with us Next Article How to Create a HashSet with a Custom Initial Load Factor in Java? F farheen_khan26 Follow Improve Article Tags : Java Java Programs HashSet java-hashset Java Examples +1 More Practice Tags : Java Similar Reads Load Factor in HashMap in Java with Examples HashMap is a class that implements the Map interface of Java Collections Framework. The most important feature of a HashMap is that it has a constant time performance for retrieval and insertion. The two factors that dictate the performance of a HashMap are: Initial CapacityLoad Factor Before we exp 5 min read How to Create a HashSet With a Predefined Capacity in Java? In Java, we can create HashSet with a predefined capacity by creating a constructor and passing the capacity as a parameter to it. We have to initialize the capacity using a variable otherwise you can pass a direct value to it. Syntax:HashSet <datatype>myset=new HashSet<>(capacity);Here, 1 min read How to Initialize HashSet Values Using Constructor in Java? In Java programming, HashSet is a collection framework that is implemented by the HashSet class. It only contains unique elements and does not allow duplicate values. Mainly it does not maintain any insertion order but is inserted based on hash code. We can initialize HashSet values in many ways in 2 min read How to Convert ArrayList to HashSet in Java? ArrayList: In Java, ArrayList can have duplicates as well as maintains insertion order. HashSet: HashSet is the implementation class of Set. It does not allow duplicates and uses Hashtable internally. There are four ways to convert ArrayList to HashSet : Using constructor.Using add() method by itera 3 min read How to Iterate Over a HashSet Without an Iterator in Java? In Java, we can iterate over a HashSet without using an Iterator. For this, we need two methods. We can directly loop through the elements of the HashSet. In this article, we will discuss the two methods to iterate over a HashSet without using Iterator. Program to Iterate over a HashSet without an I 2 min read Like