How to find / get hashmap bandwidth?

I am curious what I read in the documentation :

Capacity is the number of buckets in a hash table ... A load factor is an indication of how full a hash table is allowed before its capacity is automatically increased. When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the hash table is re-displayed (that is, the internal data structures are rebuilt), so the hash table has about twice as many buckets.

Is there a way to find out the capacity (number of buckets) of a hash card at time t?

+7
java hashmap
source share
3 answers

You need reflection

HashMap m = new HashMap(); Field tableField = HashMap.class.getDeclaredField("table"); tableField.setAccessible(true); Object[] table = (Object[]) tableField.get(m); System.out.println(table == null ? 0 : table.length); 
+11
source share

If you look at the online API , you will see that there are no public methods that will tell you. There is always reflection, but I would not recommend this.

In any case, this can be seen as an implementation detail that you should not rely on in most cases.

+4
source share

There is no such public method in HashMap .

You can use debug mode in your IDE to view HashMap.table .

plus: as Sanjeev commented, reflection is an option.

+1
source share

All Articles