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?
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.
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.
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 .
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.
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With