private static int hashCapacity ( int min ) { BigInteger capacity = BigInteger . valueOf ( min * 2 + 1 ) ; while ( ! capacity . isProbablePrime ( 10 ) ) { capacity = capacity . add ( BigInteger . valueOf ( 2 ) ) ; } return capacity . intValue ( ) ; }
Returns a prime number at least twice as large as needed . This should minimize hash collisions . Since all the hash keys are known up front the capacity could be tweaked until there are no collisions but this technique is easier and deterministic .