It's a combination of both.
There is an underlying, contiguous array that backs HashMap
. The elements of this array are actually singly linked lists. Each time you add a key-value pair to the map, the key is hashed and a linked list entry is added to the corresponding slot of the backing array (i.e. the slot corresponding to the key's hash value).
For instance, a map that maps k
to v
might look like this:
0 1 2 3 4 5 6 7 +---+---+---+---+---+---+---+---+ | | | | | | | | | +-X-+-X-+-↓-+-X-+-X-+-X-+-X-+-X-+ ↓ ↓ +---+ | k | | - | | v | +---+
There is a long "table" that backs the map, and an entry that supports the specific k
-to-v
pairing.
It would probably be best for you to take a look at the HashMap
source for yourself.