I just wish to validate my below understanding so please suggest.
In Java, regular array can have indices up to the maximum value of int
type which is 2 raised to power 31 minus -1
and since HashMap
MAXIMUM_CAPACITY
is an int
too, it can go up to that value too.
But since HashMap
internally needs table length(bucket size) to be a power of two
so limit gets curtailed to - static final int MAXIMUM_CAPACITY = 1 << 30;
since that value is nearest power of two
to 1<<31 -1
.
Am I correct in my understanding?
All answers here mention only about sign bit limit but not power of two requirement,
/**
* The table, resized as necessary. Length MUST Always be a power of two.
*/
transient Entry<K,V>[] table = (Entry<K,V>[]) EMPTY_TABLE;
Also, I understand that size limit for array
or Hashmap
(bucket size) has nothing to do with system / object / heap memory
limitations but max_range for int
data type only (index data type)and other logical requirements (like power of two etc).