44

What is the difference between HashTable and HashMap purely in context of data structures (and not in Java or any other language)?

I have seen people using these terms interchangeably for the same concept. Does it have no difference at all purely in context of data structures?

6
  • There's no standard HashTable or HashMap type in C. The two terms are usually used interchangeably. Commented Aug 28, 2015 at 15:48
  • 4
    I am aware of it that there is no such standard HashTable or HashMap in C. All I meant was when programming its concept in C is there any difference between both. Commented Aug 28, 2015 at 15:57
  • then this has nothing to do with C. C has no notion of "HashMap" or "HashTable". Commented Aug 28, 2015 at 16:06
  • The reason I mentioned C was that the question may not be mistaken as another question asking its difference in java. Commented Aug 28, 2015 at 16:12
  • 3
    Can't see how the lnk is convincing about its similarity! I don't know how is it duplicate of the link mentioned. Commented Aug 28, 2015 at 16:19

5 Answers 5

43

In Computing Science terminology, a map is an associative container mapping from a key to a value. In other words, you can do operations like "for key K remember value V" and later "for key K get the value". A map can be implemented in many ways - for example, with a (optionally balanced) binary tree, or a hash table, or even a contiguous array of structs storing the key/value.

A hash table is a structure for storing arbitrary data, and that data does not necessarily consist of a separate key and value. For example, I could have a hash table containing the values { 1, 10, 33, 97 }, which would be their own keys. When there is no value distinct from the key, this is sometimes known as a "set", and with a hash table implementation a "hash set". The defining quality of a hash table is that a hash function calculates an array index from the key data, with different keys tending to yield different indices, allowing constant time access to an array element likely to contain the key. That's an implementation / performance quality, rather than a functional quality like that defining a map.

So, a hash table stores elements, each of which need not consist of distinct key and value components, but if it does then it's also a hash map.

Sign up to request clarification or add additional context in comments.

11 Comments

So a hashmap is simply a particular type of a hash table (one which has distinct keys and values)?
@NickZuber: yes, that's correct - a reasonable paraphrasing of my final paragraph. Cheers
Thank you for finally being the first person to clearly explain what the difference between a hash table and hashmap +1
This is a high-quality answer but still misses to make the difference clear. In which situation we can implement what you explain as map(key-value) in a binary tree? why don''t people call just map instead of hashmap if the Computer Science definition is just like you explained being map? Your explanation os Hashtable is very ambiguous, it leaves margin the interpret there is no key-value, and that is not the case, your example simply communicates the key and value can be the same.
@TonyDelroy I understand, not long after posting that comment I looked into sets as data structures and was similarly stumped by their use. Why have a data structure where in order to access an element, you need to already have that same element. But it's indeed to ask: is the element in this set? I can see that being useful sometimes. It wasn't really the hash part of the data structure that stumped me, it was the functionality of sets in general. Now that I get that, this totally makes sense. A hash table is a specific implementation of a set.
|
5

Here's the way I understand it:
Hash Table: what we call the concept in Computer Science
Hash Map: what it is called in Java
Hash Set (HashSet): the case where we only care about the unique keys (or you can see it as a Hash Table where we ignore the values, we just want to know what is the set of unique keys)

Or simply,
Hash Table (CS) = HashMap (Java) = Dictionary (Python)
Hash Set (CS) = HashSet (Java) = Set (Python)

1 Comment

I understand it like that exactly as well :)
1

C doesn't have any built-in containers (apart from arrays), so it's up to the individual implementor as to what they want to call it. As far as C is concerned, HashMap vs. HashTable have no real meaning.

One possible distinction may be in how the backing store is set up. A hash table may be a simple linear array of keys and values, indexed by hash. A hash map may be a balanced tree ordered by key, along with a table that maps the hash to the tree node, allowing for both fast (O(1)) lookup and the ability to traverse the data in key order.

Or it could be something completely different. Again, C doesn't have any sort of built-in container for this sort of thing, so the names don't really mean anything in a C context.

Comments

0

The explaination between hashmap and hashtable is quite correct as it also fits to the header of a string hash map implementated in strmap.c where the stringmap is a hashtable for strings satisfying the properties of a key,value-structure. Here it says :

/*
 *    strmap version 2.0.1<br>
 *
 *    ANSI C hash table for strings.
 *
 *    Version history:
 *    1.0.0 - initial release
 *    2.0.0 - changed function prefix from strmap to sm to ensure
 *        ANSI C compatibility 
 *    2.0.1 - improved documentation<
 *
 *    strmap.c
 *
 *    Copyright (c) 2009, 2011, 2013 Per Ola Kristensson.
 *
 *    Per Ola Kristensson <[email protected]> 
 *    Inference Group, Department of Physics
 *    University of Cambridge
 *    Cavendish Laboratory
 *    JJ Thomson Avenue
 *    CB3 0HE Cambridge
 *    United Kingdom
 *
 *    strmap is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU Lesser General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    strmap is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public License
 *    along with strmap.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "strmap.h"
typedef struct Pair Pair;
typedef struct Bucket Bucket;
struct Pair {
    char *key;
    char *value;
};

Comments

-1

What I understand about the difference between Hashmap and Hashtable only from a Data Structure standpoint, disregarding technology implementing it is the following:

Hashmap: Is a higher-level Data Structure that organizes data in a key-value pair manner. Ex: yellow pages;

Hashtable: Is a type of Hashmap that the key information is directly related to the value, very often generated by applying a hashing function using the value as the source, but it doesn't have to be in order to be considered a hashtable. As mentioned above, having the same key as the value would be still consider hashtable.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.