The number of buckets is much smaller than that! The hashCode() isn't used directly as an index into a bucket; the HashMap uses the hashCode() modulo the number of buckets. The number of buckets is chosen so as to achieve a given load factor -- i.e., if the buckets start to get too full, the HashMap makes more of them.
But in any case, the number of prety much EVERYTHING is limited to that value. The size of a Java array is a 32-bit int; the length of a java.util.List is a 32-bit int. If you had a 64-bit JVM with enough RAM to hold more than 2^32 (4 billion) objects, then it'd be hard to find a container they'd fit into.
Not quite correct Ernest. Although most collections have that limitation, LinkedList can have potentially more than Integer.MAX_VALUE elements, since it does not use an array for its storage. This is also reflected by the Javadoc of Collection.size():
Returns the number of elements in this collection. If this collection contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.
However, its toArray() method is flawed because of this, since it can never contain all elements if there are more than Integer.MAX_VALUE.
Is it really 32 bits' worth of capacity Ernest, rather than 31 bits? I have tried to create an array with more than 2147483647 (2^31 - 1) members, and I get compiler errors saying the number is out of range for an int.
author and iconoclast