Reputation: 5645
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?
Upvotes: 0
Views: 362
Reputation: 11185
The Entry is a collection (a light-weight linked list of sorts. Not a java Collection
strictly speaking). Entries can link each other.
static class Entry<K,V> implements Map.Entry<K,V> {
final K key;
V value;
Entry<K,V> next;
final int hash;
}
Upvotes: 1
Reputation: 234795
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.
Upvotes: 0
Reputation: 198033
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.
Upvotes: 5