user86834
user86834

Reputation: 5645

What type is a HashMap bucket

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

Answers (3)

Deepak Bala
Deepak Bala

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

Ted Hopp
Ted Hopp

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

Louis Wasserman
Louis Wasserman

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

Related Questions