0

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?

0

3 Answers 3

5

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.

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

Comments

1

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; } 

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.