Custom HashMap implementation

This question was asked to me in an interview. I think the only way to get best solution is SOF. So the question was “How would you implement a custom HashMap in java(Assume there is no such data structure called HashMap)“. The only answer I could think of was by implementing Associative Arrays (but then again, Java doesnt have associative arrays). Could you experts please pour in your thoughts on this question?

Answer

Short Answer:

It would be an array of arrays (or Lists).

Object[][] map;

where map[bucketIndex] would return the ‘bucket’.

When inserting something you need a function to calculate bucketIndex this would use the hashcode of the inserted object.

Boom done!

🙂

Leave a Reply

Your email address will not be published. Required fields are marked *