-3

In a recent interview I was being asked how to write your own HashMap/Hashtable implementation in Java.

I have no idea of this so the only thing I said as a answer was that we can implement HashMap by using Array, because only this provides constant time access, if you know the index. Key is to writing hash function to minimize collision.

Can you please advise me how we can write our own Hashmap/Hashtable?

Roman C
  • 49,761
  • 33
  • 66
  • 176
Tuntun Gdfhjf
  • 39
  • 1
  • 1
  • 2
  • 2
    Here you go :) http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/HashMap.java. For hastable http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/Hashtable.java – M Sach Mar 17 '13 at 12:51
  • One of the first results when searching for "how to write a hash map": http://whileonefork.blogspot.de/2011/02/hashmap-101-build-your-own.html – Philipp Reichart Mar 17 '13 at 12:52
  • 2
    checkout [HashMap](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/java/util/HashMap.java) in java. – yair Mar 17 '13 at 12:52
  • 1
    This is taught in every undergraduate data structures course, and described in every introductory data structure text book. – Stephen C Mar 17 '13 at 12:58
  • This is trending java question, Even I was asked the same question in one of the interview. The answer would be create a Entry class with key value just like Entry. Then add some implementation for methods such as get(key), put(key,value), remove(key). I found out an algorithm for custom HashMap may be it can help you. Check out the link : http://www.javamadesoeasy.com/2015/02/hashmap-custom-implementation.html – Harleen Apr 06 '16 at 16:15

1 Answers1

6

Look sources to get more about how do they work.

Also, there are some good explanations at SO:

Community
  • 1
  • 1
bsiamionau
  • 8,099
  • 4
  • 46
  • 73
  • This is trending java question, Even I was asked the same question in one of the interview. The answer would be create a Entry class with key value just like Entry. Then add some implementation for methods such as get(key), put(key,value), remove(key). I found out an algorithm for custom HashMap may be it can help you. Check out the link : – Harleen Apr 06 '16 at 16:13