@@ -51,7 +51,10 @@ public HashSet() {
51
51
/**
52
52
* Adds the specified element to this set if it is not already present
53
53
* If this set already contains the element, the call leaves the set unchanged and returns false.
54
- * <p> If load factor (0.75) is exceeded, triggers a resize operation and double the current capacity.
54
+ * <p>
55
+ * If load factor (0.75) is exceeded, triggers a resize operation and double the current capacity.
56
+ * It's important to note that resizing is not performed with every add operation but rather when the load
57
+ * factor exceeds the threshold. Therefore, the amortized time complexity of adding elements remains O(1)
55
58
*
56
59
* @param element the element to be added to this set
57
60
* @return true if this set did not already contain the specified
@@ -80,6 +83,9 @@ public boolean add(T element) {
80
83
* than expected when looking for an element.
81
84
* <p>
82
85
* If load factor falls below 0.25, trigger a resize and halve the current capacity.
86
+ * It's important to note that resizing is not performed with every remove operation but rather when the
87
+ * load factor falls below a certain limit. Therefore, the amortized time complexity of removing elements
88
+ * remains O(1)
83
89
*
84
90
* @param element the element to be removed from this set, if present
85
91
* @return true if this set contained the specified element
@@ -275,6 +281,9 @@ private int capacity() {
275
281
* If the load factor is exceeded, the capacity is increased by doubling it (possibly triggered after an add operation),
276
282
* or if the load factor falls below 1/4 (arbitrary) of the capacity (and the capacity is larger than the minimum capacity), the
277
283
* capacity is decreased by halving it (possibly triggered after a remove operation).
284
+ * <p>
285
+ * The resizing operation involves rehashing all existing elements into a new array with the updated capacity.
286
+ * This process takes O(n) time, where n is the number of elements in the hash set.
278
287
*/
279
288
private void resize (int newCapacity ) {
280
289
// creates a temporary reference to the original bucket
0 commit comments