public static int hashTableSize ( int numElements ) throws IllegalArgumentException { if ( numElements < 0 ) { throw new IllegalArgumentException ( "cannot be negative; numElements=" + numElements ) ; } else if ( numElements > HASH_TABLE_MAX_SIZE ) { throw new IllegalArgumentException ( "exceeds maximum number of buckets; numElements=" + numElements ) ; } if ( numElements == 0 ) return 1 ; if ( numElements < 3 ) return numElements * 2 ; // Apply load factor to number of elements and determine next // largest power of 2 that fits in an int int sizeAfterLoadFactor = ( int ) ( ( long ) numElements * 10 / 7 ) ; int bits = 32 - Integer . numberOfLeadingZeros ( sizeAfterLoadFactor - 1 ) ; return 1 << bits ; }
Determine size of hash table capable of storing the specified number of elements with a load factor applied .