36

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?

Jonathan Lam
  • 16,831
  • 17
  • 68
  • 94
Rajan Kalra
  • 433
  • 1
  • 5
  • 13
  • There's no standard HashTable or HashMap type in C. The two terms are usually used interchangeably. – The Paramagnetic Croissant Aug 28 '15 at 15:48
  • 3
    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. – Rajan Kalra Aug 28 '15 at 15:57
  • then this has nothing to do with C. C has no notion of "HashMap" or "HashTable". – The Paramagnetic Croissant Aug 28 '15 at 16:06
  • The reason I mentioned C was that the question may not be mistaken as another question asking its difference in java. – Rajan Kalra Aug 28 '15 at 16:12
  • As pointed out there are no "HashMap"/"HashTable" in iso standard C. There is a hashmap in the boost lib for C++ though. Also, this question is a Duplicate of http://stackoverflow.com/questions/3578083/what-is-the-best-way-to-use-a-hashmap-in-c – Dwight Spencer Aug 28 '15 at 16:14
  • 3
    Can't see how the lnk is convincing about its similarity! I don't know how is it duplicate of the link mentioned. – Rajan Kalra Aug 28 '15 at 16:19

5 Answers5

37

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.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
  • 2
    So a `hashmap` is simply a particular type of a `hash table` (one which has distinct keys and values)? – Nick Zuber Jan 01 '16 at 18:54
  • 2
    @NickZuber: yes, that's correct - a reasonable paraphrasing of my final paragraph. Cheers – Tony Delroy Jan 02 '16 at 16:17
  • 4
    Thank you for finally being the first person to clearly explain what the difference between a hash table and hashmap +1 – Nick Zuber Jan 02 '16 at 17:13
  • 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. – RollRoll Jan 22 '21 at 11:17
  • @RollRoll: I've reread my answer and I think it's very clear, but I can see English isn't your first language, so let's work through your questions: _"In which situation we can implement what you explain as map(key-value) in a binary tree?"_ - you can do that whenever the memory usage and performance implications of a binary tree are ok for your application. Using a tree (e.g. C++ `std::map`), you'll have O(log N) lookup, insertion and erase, whereas a hash table (`std::unordered_map`) has amortised O(1). Using a vector is most CPU cache friendly, and typically has lowest memory overheads. – Tony Delroy Jan 22 '21 at 22:44
  • _"why don't people call just map instead of hashmap if the Computer Science definition is just like you explained being map?"_ - if people know what they're talking about, they'll use "map" whenever they only care that it's an associative container (i.e. they can map from keys to values). They should only say "hashmap" if the implementation uses a hash table. – Tony Delroy Jan 22 '21 at 22:45
  • _"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."_ - your complaint is ambiguous, what do you mean by _"no key-value"_? I have given an example `{ 1, 10, 33, 96 }` showing a hash table _may_ store a set of elements in which there are no distinct key and value components. Obviously given hashmaps exist, you can also store something like `{ 1->"one", 2->"two", 3->"three" }` (to pick an arbitrary representation), _with_ distinct keys and values. – Tony Delroy Jan 22 '21 at 22:46
6

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)

cryanbhu
  • 4,780
  • 6
  • 29
  • 47
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.

John Bode
  • 119,563
  • 19
  • 122
  • 198
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 <pok21@cam.ac.uk> 
 *    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;
};
jotasi
  • 5,077
  • 2
  • 29
  • 51
Marvin M
  • 21
  • 2
-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.

RollRoll
  • 8,133
  • 20
  • 76
  • 135