Create a Hash Table with two arrays

23,764

Actually, some of todays Hashmap implentations are indeed made out of arrays as you propose. Let me sketch how this works:

Hash Function A hash function transforms your keys into an index for the first array (array K). A hash function such as MD5 or a simpler one, usually including a modulo operator, can be used for this.

Buckets A simple array-based Hashmap implementation could use buckets to cope with collissions. Each element ('bucket') in array K contains itself an array (array P) of pairs. When adding or querying for an element, the hash function points you to the correct bucket in K, which contains your desired array P. You then iterate over the elements in P until you find a matching key, or you assign a new element at the end of P.

Mapping keys to buckets using the Hash You should make sure that the number of buckets (i.e. the size of K) is a power of 2, let's say 2^b. To find the correct bucket index for some key, compute Hash(key) but only keep the first b bits. This is your index when cast to an integer.

Rescaling Computing the hash of a key and finding the right bucket is very quick. But once a bucket becomes fuller, you will have to iterate more and more items before you get to the right one. So it is important to have enough buckets to properly distribute the objects, or your Hashmap will become slow.

Because you generally don't know how much objects you will want to store in the Hashmap in advance, it is desirable to dynamically grow or shrink the map. You can keep a count of the number of objects stored, and once it goes over a certain threshold you recreate the entire structure, but this time with a larger or smaller size for array K. In this way some of the buckets in K that were very full will now have their elements divided among several buckets, so that performance will be better.

Alternatives You may also use a two-dimensional array instead of an array-of-arrays, or you may exchange array P for a linked list. Furthermore, instead of keeping a total count of stored objects, you may simply choose to recreate (i.e. rescale) the hashmap once one of the buckets contains more than some configured number of items.

A variation of what you are asking is described as 'array hash table' in the Hash table Wikipedia entry.

Code For code samples, take a look here.

Hope this helps.

Share:
23,764
locoboy
Author by

locoboy

Love any kind of something that is about engineering

Updated on July 17, 2020

Comments

  • locoboy
    locoboy almost 4 years

    Does anyone know how to do this and what the pseudo code would look like?

    As we all know a hash table stores key,value pairs and when a key is a called, the function will return the value associated with that key. What I want to do is understand the underlying structure in creating that mapping function. For example, if we lived in a world where there were no previously defined functions except for arrays, how could we replicate the Hashmaps that we have today?

  • Romain Linsolas
    Romain Linsolas over 13 years
    Yes, indeed, I didn't see that. I've edited my answer, but the main part is not really specific to Java.
  • sepp2k
    sepp2k over 13 years
    I'm pretty sure he wants to create his own implementation of a hash table using two arrays.
  • locoboy
    locoboy over 13 years
    thanks, but where exactly are you defining the hashing function? to my knowledge you need a hashing function, two arrays and a way to get rid of collisions.
  • locoboy
    locoboy over 13 years
    yes I'm looking to create my own implementation of a hash table. I don't want to use any previously defined objects. I assume that we will need a hashing function (to generate values for value indicies), two arrays (to store keys and values), and a way to get/resolve collsions.