I've been digging into the HashMap implementation in Java. All values are stored in a 'bucket' that is an Entry object. I was expecting it to be a collection or am I missing something here?
3 Answers
Nope. Since it doesn't have to let users access the buckets through the API, HashMap gets reduced memory usage and a simpler implementation by just rolling its own very small linked list implementation internally. It could use LinkedList, but it has no need of a doubly-linked list, and it's more efficient to pack the link, the key, the value, the key's hash code, etc. into one object.
Comments
I don't know whose implementation you are looking at, but in the OpenJDK 6 version, it's clear that an Entry stores a single value and that each Entry forms a node in a linked list.