Rehashing in JavaIn the data structure, hashing is the most important concept that is used to convert a given key into another value. The new value can be generated by using the hash function. In this section, we will understand the concept of rehashing in Java along with the load factor and hashing concept. RehashingRehashing is a technique that dynamically expands the size of the Map, Array, and Hashtable to maintain the get and put operation complexity of O(1). It can be also defined as rehashing is the process of recalculating the hash code of already stored entries and moving them to a bigger size hash map when the number of elements in the map reaches the maximum threshold value. In simple words, rehashing is the reverse of hashing process. It retains the performance. In rehashing, the load factor plays a vital role. Load FactorThe load factor is a measure that decides when to increase the HashMap or Hashtable capacity to maintain the get() and put() operation of complexity O(1). The default value of the load factor of HashMap is 0.75 (75% of the map size). In short, we can say that the load factor decides "when to increase the number of buckets to store the keyvalue pair." LARGER load factor: Lower space consumption but higher lookups SMALLER Load factor: Larger space consumption compared to the required number of elements. How Load Factor is Calculated? Load factor can be calculated by using the following formula: For example: The initial capacity of HashMap is = 16 It represents that the 12^{th} keyvalue pair of HashMap will keep its size to 16. As soon as the 13^{th }element (keyvalue pair) will come into the HashMap, it will increase its size from default 2^{4} = 16 buckets to 2^{5} = 32 buckets. Load Factor ExampleLet's understand the load factor through an example. We know that the default bucket size of the HashMap is 16. We insert the first element, now check whether we need to increase the HashMap capacity or not. It can be determined by the formula: Size of the HashMap (m) / Number of Buckets (n) In this case, the size of the HashMap is 1, and the bucket size is 16. So, 1/16 = 0.0625. Now compare the obtained value with the default load factor (0.75). 0.0625 < 0.75 The value is smaller than the default value of the load factor. So, no need to increase the HashMap size. Therefore, we do not need to increase the size of the HashMap up to the 12^{th} element because 12/16 = 0.75 The obtained value is equal to the default load factor, i.e., 0.75. As soon as, we insert the 13^{th} element in the HashMap, the size of HashMap is increased because: 13/16 = 0.8125 Here, the obtained value is greater than the default load factor value. 0.8125 > 0.75 In order to insert the 13^{th} element into the HashMap, we need to increase the HashMap size. If you want to keep get() and put() operation complexity O(1), it is advisable to have a load factor around 0.75. Why rehashing is required?Rehashing is required when the load factor increases. The load factor increases when we insert keyvalue pair in the map and it also increases the time complexity. Generally, the time complexity of HashMap is O(1). In order to reduce the time complexity and load factor of the HashMap, we implement the rehashing technique. We should use rehashing for the following conditions:
Implementing RehashingThe following steps must follow to implement the rehashing process:
Since the cost of rehashing of N elements will be: Rehashing Java ProgramLet's implement the rehashing technique in a Java program. RehashingExample.java Output: HashMap created Number of pairs in the Map: 0 Size of Map: 5 Default Load Factor : 0.75 Pair(1, Hyundai) inserted successfully. Current Load factor = 0.2 Number of pairs in the Map: 1 Size of Map: 5 Current HashMap: key = 1, val = Hyundai Pair(2, KIA) inserted successfully. Current Load factor = 0.4 Number of pairs in the Map: 2 Size of Map: 5 Current HashMap: key = 1, val = Hyundai key = 2, val = KIA Pair(3, Toyota) inserted successfully. Current Load factor = 0.6 Number of pairs in the Map: 3 Size of Map: 5 Current HashMap: key = 1, val = Hyundai key = 2, val = KIA key = 3, val = Toyota Pair(4, Mahindra) inserted successfully. Current Load factor = 0.8 0.8 is greater than 0.75 Therefore Rehashing will be done. ***Rehashing Started*** Pair(1, Hyundai) inserted successfully. Current Load factor = 0.1 Number of pairs in the Map: 1 Size of Map: 10 Pair(2, KIA) inserted successfully. Current Load factor = 0.2 Number of pairs in the Map: 2 Size of Map: 10 Pair(3, Toyota) inserted successfully. Current Load factor = 0.3 Number of pairs in the Map: 3 Size of Map: 10 Pair(4, Mahindra) inserted successfully. Current Load factor = 0.4 Number of pairs in the Map: 4 Size of Map: 10 ***Rehashing Done*** New Size of Map: 10 Number of pairs in the Map: 4 Size of Map: 10 Current HashMap: key = 1, val = Hyundai key = 2, val = KIA key = 3, val = Toyota key = 4, val = Mahindra Pair(5, Jeep) inserted successfully. Current Load factor = 0.5 Number of pairs in the Map: 5 Size of Map: 10 Current HashMap: key = 1, val = Hyundai key = 2, val = KIA key = 3, val = Toyota key = 4, val = Mahindra key = 5, val = Jeep Pair(6, Ford) inserted successfully. Current Load factor = 0.6 Number of pairs in the Map: 6 Size of Map: 10 Current HashMap: key = 1, val = Hyundai key = 2, val = KIA key = 3, val = Toyota key = 4, val = Mahindra key = 5, val = Jeep key = 6, val = Ford Pair(7, BMW) inserted successfully. Current Load factor = 0.7 Number of pairs in the Map: 7 Size of Map: 10 Current HashMap: key = 1, val = Hyundai key = 2, val = KIA key = 3, val = Toyota key = 4, val = Mahindra key = 5, val = Jeep key = 6, val = Ford key = 7, val = BMW Pair(8, Audi) inserted successfully. Current Load factor = 0.8 0.8 is greater than 0.75 Therefore Rehashing will be done. ***Rehashing Started*** Pair(1, Hyundai) inserted successfully. Current Load factor = 0.05 Number of pairs in the Map: 1 Size of Map: 20 Pair(2, KIA) inserted successfully. Current Load factor = 0.1 Number of pairs in the Map: 2 Size of Map: 20 Pair(3, Toyota) inserted successfully. Current Load factor = 0.15 Number of pairs in the Map: 3 Size of Map: 20 Pair(4, Mahindra) inserted successfully. Current Load factor = 0.2 Number of pairs in the Map: 4 Size of Map: 20 Pair(5, Jeep) inserted successfully. Current Load factor = 0.25 Number of pairs in the Map: 5 Size of Map: 20 Pair(6, Ford) inserted successfully. Current Load factor = 0.3 Number of pairs in the Map: 6 Size of Map: 20 Pair(7, BMW) inserted successfully. Current Load factor = 0.35 Number of pairs in the Map: 7 Size of Map: 20 Pair(8, Audi) inserted successfully. Current Load factor = 0.4 Number of pairs in the Map: 8 Size of Map: 20 ***Rehashing Done*** New Size of Map: 20 Number of pairs in the Map: 8 Size of Map: 20 Current HashMap: key = 1, val = Hyundai key = 2, val = KIA key = 3, val = Toyota key = 4, val = Mahindra key = 5, val = Jeep key = 6, val = Ford key = 7, val = BMW key = 8, val = Audi Pair(9, MercedesBenz) inserted successfully. Current Load factor = 0.45 Number of pairs in the Map: 9 Size of Map: 20 Current HashMap: key = 1, val = Hyundai key = 2, val = KIA key = 3, val = Toyota key = 4, val = Mahindra key = 5, val = Jeep key = 6, val = Ford key = 7, val = BMW key = 8, val = Audi key = 9, val = MercedesBenz Pair(10, Ferrari) inserted successfully. Current Load factor = 0.5 Number of pairs in the Map: 10 Size of Map: 20 Current HashMap: key = 1, val = Hyundai key = 2, val = KIA key = 3, val = Toyota key = 4, val = Mahindra key = 5, val = Jeep key = 6, val = Ford key = 7, val = BMW key = 8, val = Audi key = 9, val = MercedesBenz key = 10, val = Ferrari
Next TopicRound Robin Scheduling Program in Java
