Rajan Kalra
Rajan Kalra

Reputation: 483

Difference between HashMap and HashTable purely in Data Structures

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?

Upvotes: 41

Views: 12155

Answers (5)

Tony Delroy
Tony Delroy

Reputation: 106096

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.

Upvotes: 41

RollRoll
RollRoll

Reputation: 8462

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.

Upvotes: -1

cryanbhu
cryanbhu

Reputation: 5254

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)

Upvotes: 5

Marvin M
Marvin M

Reputation: 21

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;
};

Upvotes: 0

John Bode
John Bode

Reputation: 123458

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.

Upvotes: 1

Related Questions