1

Why does HashMap in java internally use array to store Entry Objects and not an ArrayList ?

1 Answer 1

2

The reason for this is highly likely that HashMap needs to control how its internal table is resized according to the number of entries and the given loadFactor.

Since ArrayList doesn't expose methods to resize its internal array to specific sizes (HashMap uses powers of 2 for its size to optimize rehashing, but ArrayList multiplies capacity by 1.5), it simply wasn't an option to be considered.

Also, even if ArrayList did increase capacity in the same way, relying on this internal detail would tie these two classes together, leaving no room to change the internal implementation of ArrayList at a later date as it could break HashMap or at the very least make it less memory efficient.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.