|
19 | 19 | * List<T> toList() returns a List representation of this HashSet. O(n).
|
20 | 20 | * int size() gets the number of elements (cardinality) in this HashSet. O(1).
|
21 | 21 | * boolean isEmpty() checks if the HashSet is empty. O(1).
|
| 22 | + * int capacity() returns the capacity of this HashSet. O(1). |
22 | 23 | *
|
23 | 24 | * @param <T> the type of objects that are contained within this HashSet. T must override
|
24 | 25 | * Object::equals and Object::hashCode for the methods add, remove, and contains to be well-defined.
|
@@ -152,6 +153,16 @@ public List<T> toList() {
|
152 | 153 | .collect(Collectors.toList());
|
153 | 154 | }
|
154 | 155 |
|
| 156 | + /** |
| 157 | + * Returns the number of buckets of this HashSet. Equivalently, returns the maximum number of elements that can |
| 158 | + * be stored in this HashSet. |
| 159 | + * |
| 160 | + * @return the number of buckets of this HashSet. |
| 161 | + */ |
| 162 | + public int capacity() { |
| 163 | + return this.buckets.length; // returns the number of buckets. |
| 164 | + } |
| 165 | + |
155 | 166 | /**
|
156 | 167 | * Hashes the specified element to determine the bucket index for placement within the array.
|
157 | 168 | * The hash function calculates the index by performing the following steps:
|
@@ -267,16 +278,6 @@ private boolean isTombstoneBucket(int bucketIndex) {
|
267 | 278 | return this.TOMBSTONE.equals(this.buckets[bucketIndex]);
|
268 | 279 | }
|
269 | 280 |
|
270 |
| - /** |
271 |
| - * Returns the number of buckets of this HashSet. Equivalently, returns the maximum number of elements that can |
272 |
| - * be stored in this HashSet. |
273 |
| - * |
274 |
| - * @return the number of buckets of this HashSet. |
275 |
| - */ |
276 |
| - private int capacity() { |
277 |
| - return this.buckets.length; // returns the number of buckets. |
278 |
| - } |
279 |
| - |
280 | 281 | /**
|
281 | 282 | * If the load factor is exceeded, the capacity is increased by doubling it (possibly triggered after an add operation),
|
282 | 283 | * or if the load factor falls below 1/4 (arbitrary) of the capacity (and the capacity is larger than the minimum capacity), the
|
|
0 commit comments