Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 579
Rajan Kalra Avatar asked Aug 28 '15 15:08

Rajan Kalra


People also ask

What is the difference between HashMap and Hashtable data structure?

Though both Hashtable and HashMap are data-structure based upon hashing and implementation of Map interface, the main difference between them is that HashMap is not thread-safe but Hashtable is thread-safe. This means you cannot use HashMap in a multi-threaded Java application without external synchronization.

What is the difference between Hashtable and HashMap Mcq?

HashMap Vs HashTable In Java : HashMap is not synchronized and therefore it is not thread safe. HashTable is internally synchronized and therefore it is thread safe. HashMap allows maximum one null key and any number of null values. HashTable doesn't allow null keys and null values.

What is the difference between HashMap Hashtable and HashSet?

Hashtable and HashMap both implement Map , HashSet implements Set , and they all use hash codes for keys/objects contained in the sets to improve performance. Hashtable is a legacy class that almost always should be avoided in favor of HashMap .


3 Answers

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.

like image 64
Tony Delroy Avatar answered Nov 12 '22 11:11

Tony Delroy


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)

like image 26
cryanbhu Avatar answered Nov 12 '22 12:11

cryanbhu


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.

like image 21
John Bode Avatar answered Nov 12 '22 12:11

John Bode