3

During my most recent job interview for a software engineer position, I was asked this questions: what are the differences between hashtable and hashmap? I asked the interviewer if he was specific about Java since in Java hashtable is synchronized and hashmap is not (and actually tons of information to compare hashtable vs hashmap in Java after googling so that's not the answer I am looking for) but he said no and wanted to me to explain the the difference of these two in general.

I was really puzzled and shocked (actually still puzzled now) about this question. IMO, hastable or hashmap is simply a matter of terminology. Actually only Java has both terms and in other languages like C++, they don't even have the term hashtable. During the interview, I just explained the principle of hashing and said that hashmap and hashtable should both be implemented based on this principle and I don't know if there is any difference between these two. The interviewer was definitely not convinced and was looking for other answers and of course I was rejected after that round.

So back to the topic, what could possibly be the differences between hashmap and hashtable in general (not specific to Java) if there is any?

Optimus Prime
  • 409
  • 7
  • 15

3 Answers3

6

In Computer Science there is a difference due to the wording.

A HashTable is some kind of lookup table using key hashes to lookup the corresponding value in a table like data structure. Thats only one kind of a key-value Mapping. There are different implementations as you are probably aware. Different hashes, hash collusion solutions and table growing strategies and more under the hood. It's only interesting if you need to make your own hash table for whatever reason.

A HashMap is some kind of mapping of key-value pairs with a hashed key. Mapping is abstract as such and it may not be a table. Balanced trees or tries or other data structures/mappings are possible too.

You could simplify and say that a HashTable is the underlying data structure and the HashMap may be utilizing a HashTable.

A Dictionary is yet another abstraction level since it may not use hashes at all - for example with full text binary search lookups or other ways for compares. This is all you can get out of the words without considering certain programming languages.

-- Before thinking too much about it. Can you say - with certainty - that your interviewer had a clue about what he/she was talking about? Did you discuss technical details or did they just listen/ask and sometimes comment? Sometimes interviewers just come up with the most ridicules answers to problems they don't really understand in the first place. Like you wrote yourself, in general it's just Terminology. Software Developers often use the Terms interchangeable, except maybe those who really have differences like in Java.

makadev
  • 1,034
  • 15
  • 26
  • Thanks for your reply. I think you answer is very good but I can't yet mark it as right answer now. To clarify things, this questions was an isolated question without any background and the interviewer just threw it up to me. Also, I am very confident that he didn't make the this question up because others mentioned this questions interviewed for the same company. As I mentioned in my post, I explained the principle of hashing to him and said hashtable and hashmap could both be implemented this way (I know you can use binary search tree to implement hasmap too but I didn't mention that)... – Optimus Prime Mar 30 '16 at 17:38
  • (to continue) and asked the interviewer if he can give me any hint about the answer he is looking for. He didn't give me any hint instead asking me another question. I am really disappointed about the results since the company I interviewed is a prestigious company in its industry and they conducted interview like this. – Optimus Prime Mar 30 '16 at 17:42
  • I see. [Here](http://stackoverflow.com/a/32367030/3828957) is another - older - answer on the plain data structure level and [this](http://stackoverflow.com/q/30110252/3828957) covers a bit more in it's answers. Good Luck finding your answer. It will be hard if all you can do is assume what is right or has enough complexity/depth to be an answers. – makadev Mar 30 '16 at 18:31
  • Thanks for the links. Probably I won't find any better answer to this questions, I will mark your answer as the right one. Again, I am very disappointed about this question. Knowing the answer will make you a better programmer? I doubt. – Optimus Prime Mar 30 '16 at 19:15
  • I doubt that too. My comment about the interviewer was from experience (on both sides). Often you get people from HR or some Head of Whatever that want specific answers which may not make sense at all. Sometimes they ask weird stuff and want to see your reaction when you're of balance. If it keeps bugging you, write them an e-mail/message. Ask for feedback, show that you care and that you evaluated the interview afterwards. Be nice, short, ask specific. At last you can show them what they lose without you. ;) – makadev Mar 30 '16 at 20:06
1

The interviewer may have been looking for the insight that...

  • a hash table is a lower-level concept that doesn't imply or necessarily support any distinction or separation of keys and values (i.e. you can implement a hash set of values using a hash table), while
  • a hash map must support distinct keys and values, as there's to be a mapping/association from keys to values; the two are distinct, even if in some implementations they're always stored side by side in memory, e.g. members of the same structure / std::pair<>.

Example: a (bad) hash table implementation preventing use as a hash map.

Consider:

template <typename T>
class Hash_Table
{
    ...
    bool insert(const T& t)
    {
        // work out which bucket t hashes to...
        size_t bucket = hash_bytes((void*)&t, sizeof t) % num_buckets_;

        // see if t is already stored in the bucket...
        if (memcmp((void*)&t, (void*)&buckets_[bucket], sizeof t) == 0)
            ...
        ... handle collisions etc. ...
    }
    ...
};

Above, the hard-coded calls to a hash function that treats the value being inserted as a binary blob, and memcmp of the entire t, mean you can't make T say a std::pair<int, std::string> and use the hash table as a hash map from ints to strings. So, it's an example of a hash table that's not usable as a hash map.


You might or might not also consider a hash table that simply doesn't provide any convenience features for use as a hash map not to be a hash map. For example, if the API was designed as if dealing only in values - h.insert(t); h.erase(t); auto i = h.find(t); - but it allowed the caller to specify arbitrary custom comparison and hashing functions that could restrict their operations to only the key part of t, then the hash table could be (ab)used as a functional hash map.


To clarify how this relates to makadev's existing answer, I disagree with:

  • "A HashTable [uses] key hashes to lookup the corresponding value"; wrong because it assumes a key->value mapping.

  • "A HashMap [...]. Mapping is abstract as such and it may not be a table. Balanced trees or tries or other data structures/mappings are possible too."; wrong because the primary mechanism of a hash map is still hashing of the key to a bucket (index) in the table/array: some hash tables/maps may use other data structures (arrays, linked lists, trees...) to store elements that collide at the same bucket, but that's a different issue and not part of the difference between hash tables and hash maps.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
  • A valid view but swinging back to the point "... it's just Terminology". Your HashTable is not what I call [the Data-structure HashTable](https://en.wikipedia.org/wiki/Hash_table). Likewise your HashMap is not what I meant with an Abstracted Mapping via Hashed Keys but more what I know as HashTable except the "not implied support of distinction or separation of keys and values". Btw. that actually doesn't make sense to me. Why would you want a Lookup Data-structure like a HashTable or HashSet without distinction - f.e. for existence check - of the keys? – makadev Apr 03 '16 at 15:09
  • @makadev: " it's just terminology" - having shared understanding of data structure, design pattern etc. terminology lets us describe & discuss systems succinctly & meaningfully. The more you're coding systems that others have to help maintain, evolve or use, the more it matters. Anyway, *"Why would you want a Lookup Data-structure like a HashTable or HashSet without distinction - f.e. for existence check - of the keys?"* - a hash set might store say words seen in a text file: there's no distinct key vs. value - each word is both key *and* value. Not saying word1 is indistinct from word2. – Tony Delroy Apr 04 '16 at 08:32
-2

Actually HashTable become obsoletes and HasHMap is best approach to use because Hashtable is synchronized. If a thread-safe implementation is not needed, it is recommended to use HashMap in place of Hashtable. If a thread-safe highly-concurrent implementation is desired, then it is recommended to use java.util.concurrent.ConcurrentHashMap in place of Hashtable.

Second difference is HashMap extends Map Interface and whether HashSet Dictionary interface.

Yasir Shabbir Choudhary
  • 2,458
  • 2
  • 27
  • 31