The value "strength 2" in java.util.HashMap

Possible duplicate:
Initial default Java HashMap capacity

I read the HashMap implementation in java.util.HashMap. Initial capacity, maximum capacity, etc. Equal to two.

Parts of an ad 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;

Comments suggest that dimensions MUST be a force of two. Why is the power of two so important?

+5
source share
2 answers

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

eg. to find a bucket from a hash code, it can use hash & (SIZE -1)insteadabs(hash) % SIZE

+14
source

, , . , , , .

, , , -, (32 ) k-, k - log (N), N - .

+1

All Articles