7

Possible Duplicate:
Java HashMap Default Initial Capacity

I was reading the implementation of HashMap in java.util.HashMap. The initial capacity,maximum capacity etc., are powers of two.

Parts of declaration copied from java.util.HashMap

/**
 * The default initial capacity - MUST be a power of two.
 */
static final int DEFAULT_INITIAL_CAPACITY = 16;


 /**
 * The maximum capacity, used if a higher value is implicitly specified
 * by either of the constructors with arguments.
 * MUST be a power of two <= 1<<30.
 */
static final int MAXIMUM_CAPACITY = 1 << 30;


/**
 * The table, resized as necessary. Length MUST Always be a power of two.
 */
transient Entry[] table;

The comments suggest the sizes MUST be a power of two. Why is power of two given so much importance ?

Community
  • 1
  • 1
Vinoth Kumar C M
  • 9,668
  • 27
  • 81
  • 125
  • 1
    Some types of hash tables use some of the bits from the bit pattern of the calculated hash value, as the index into the array. In that case, the size has to be a power of two. It seems that the implementation of HashMap works that way. – Thomas Padron-McCarthy May 17 '12 at 08:56

2 Answers2

17

Using powers of two simplifies the implementation and improves its performance.

E.g. to find a bucket from a hash code it can use hash & (SIZE -1) instead of abs(hash) % SIZE

Peter Lawrey
  • 498,481
  • 72
  • 700
  • 1,075
1

Theoretically speaking, we can amortize the cost of expanding the list only if the operation becomes negligible as the number of elements in the map increases. Doubling the size every time the load factor is reached is one way of ensuring the expansion and reloading of entries is amortized.

The reason why it is specifically a power of two initially is so that when we hash an element, the resulting integer (32-bits) can be truncated to the first k-bits, where k is the log (N) where N is the current capacity.

Kaushik Shankar
  • 4,940
  • 4
  • 27
  • 34
  • 1
    Both points are correct, though it is worth noting that the 2nd point is relatively unimportant in performance terms. – Stephen C May 17 '12 at 09:07