If you investigate the source code for HashMap, you will find that internally it always uses a capacity which is a power of two. Whatever capacity you request, the code will find the smallest power of two which is >= the number you requested, and use that instead:
So there's really no point to trying to find a prime capacity; it won't be used.
No. The power-of-two HashMap implementation showed up in JDK 1.4 and was refined a bit in JDK 1.5/5. Seems to still be used in JDK 6. It's not guaranteed to be there in the future. But unless you're targeting a specific JDK which is known to allow precise setting of the capacity, I wouldn't bother trying to optimize this.
If you were using a JDK for which prime capacities were optimal, I think it might make sense to simply precalculate an array of prime sizes to choose from. You could never have more than Integer.MAX_INTEGER capacity anyway, right? A list of thirty or so prime numbers ought to do the trick, with each one roughly double the size of the previous one. So rather than call BigInteger.nextProbablePrime(), you'd just look up the next size in your list. Ought to be much quicker, I'd think.
If a HashMap needs to hold X objects, doesn't the capacity need to be set to at least X / loadfactor?
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Yes. Well, "need" may be arguable in some contexts, but (a) it's a good idea since performace will probably decrease otherwise, and (b) it's sorta required by the API for HashMap. Which is to say that HashMap will take care of this for us anyway, if we are indeed using a plain vanilla HashMap from the JDK.. But I understood that the question we were addressing was, is it worthwhile to pre-emptively set the capacity? (To something greater than the minimum allowed by the load factor.) And if we do so, is it worthwhile to set it to a prime number? My answer being no, unless we're talking about some particular hashtable implementation other than the ones Sun privides in JDK 1.4+.