You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/main/java/com/thealgorithms/datastructures/hashmap/Readme.md
+8-8Lines changed: 8 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,27 +27,27 @@ Internally HashMap contains an array of Node and a node is represented as a clas
27
27
- V value
28
28
- Node next
29
29
30
-
It can be seen that the node is containing a reference to its own object. So it’s a linked list.
30
+
It can be seen that the node contains a reference to its object. So it’s a linked list.
31
31
32
32
## Performance of HashMap
33
-
Performance of HashMap depends on 2 parameters which are named as follows:
33
+
The performance of HashMap depends on 2 parameters which are named as follows:
34
34
- Initial Capacity
35
35
- Load Factor
36
36
37
37
38
-
**Initial Capacity**: It is the capacity of HashMap at the time of its creation (It is the number of buckets a HashMap can hold when the HashMap is instantiated). In java, it is 2^4=16 initially, meaning it can hold 16 key-value pairs.
38
+
**Initial Capacity**: It is the capacity of HashMap at the time of its creation (It is the number of buckets a HashMap can hold when the HashMap is instantiated). In Java, it is 2^4=16 initially, meaning it can hold 16 key-value pairs.
39
39
40
-
**Load Factor**: It is the percent value of the capacity after which the capacity of Hashmap is to be increased (It is the percentage fill of buckets after which Rehashing takes place). In java, it is 0.75f by default, meaning the rehashing takes place after filling 75% of the capacity.
40
+
**Load Factor**: It is the percent value of the capacity after which the capacity of Hashmap is to be increased (It is the percentage fill of buckets after which Rehashing takes place). In Java, it is 0.75f by default, meaning the rehashing takes place after filling 75% of the capacity.
41
41
42
-
**Threshold**: It is the product of Load Factor and Initial Capacity. In java, by default, it is (16 * 0.75 = 12). That is, Rehashing takes place after inserting 12 key-value pairs into the HashMap.
42
+
**Threshold**: It is the product of Load Factor and Initial Capacity. In Java, by default, it is (16 * 0.75 = 12). That is, Rehashing takes place after inserting 12 key-value pairs into the HashMap.
43
43
44
-
**Rehashing** : It is the process of doubling the capacity of the HashMap after it reaches its Threshold. In java, HashMap continues to rehash(by default) in the following sequence – 2^4, 2^5, 2^6, 2^7, …. so on.
44
+
**Rehashing** : It is the process of doubling the capacity of the HashMap after it reaches its Threshold. In Java, HashMap continues to rehash(by default) in the following sequence – 2^4, 2^5, 2^6, 2^7, …. so on.
45
45
46
-
If the initial capacity is kept higher then rehashing will never be done. But by keeping it higher increases the time complexity of iteration. So it should be chosen very cleverly to increase performance. The expected number of values should be taken into account to set the initial capacity. The most generally preferred load factor value is 0.75 which provides a good deal between time and space costs. The load factor’s value varies between 0 and 1.
46
+
If the initial capacity is kept higher then rehashing will never be done. But keeping it higher increases the time complexity of iteration. So it should be chosen very cleverly to increase performance. The expected number of values should be taken into account to set the initial capacity. The most generally preferred load factor value is 0.75 which provides a good deal between time and space costs. The load factor’s value varies between 0 and 1.
47
47
48
48
```
49
49
Note: From Java 8 onward, Java has started using Self Balancing BST instead of a linked list for chaining.
50
-
The advantage of self-balancing bst is, we get the worst case (when every key maps to the same slot) search time is O(Log n).
50
+
The advantage of self-balancing bst is, that we get the worst case (when every key maps to the same slot) search time is O(Log n).
51
51
```
52
52
53
53
Java has two hash table classes: HashTable and HashMap. In general, you should use a HashMap.
0 commit comments