hashcode()
returns interger in java so you have to map integer range to bucket size. If you are mapping from bigger set to a smaller set so you will always have collisions.
If you look at HashMap source code you will find following method to map int to bucket length.
static int indexFor(int h, int length) {
return h & (length-1);
}
The hash code is preprocessed to produce uniform distribution using:
static int hash(int h) {
// This function ensures that hashCodes that differ only by
// constant multiples at each bit position have a bounded
// number of collisions (approximately 8 at default load factor).
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
}
Applies a supplemental hash function to a given hashCode, which defends against poor quality hash functions. This is critical because HashMap uses power-of-two length hash tables, that otherwise encounter collisions for hashCodes that do not differ in lower bits. Note: Null keys always map to hash 0, thus index 0.
HashMap source