What is the difference between Hash
and Dictionary
?
Coming from a scripting background, I feel that they are similar, but I wanted to find out the exact differences. Googling did not help me much.
What is the difference between Hash
and Dictionary
?
Coming from a scripting background, I feel that they are similar, but I wanted to find out the exact differences. Googling did not help me much.
Hash
is an extremely poorly named data structure where the programmer has confused the interface with implementation (and was too lazy to write the full name, i.e. HashTable
instead resorting to an abbreviation, Hash
).
Dictionary
is the “correct” name of the interface (= the ADT), i.e. an associative container that maps (usually unique) keys to (not necessarily unique) values.
A hash table is one possible implementation of such a dictionary that provides quite good access characteristics (in terms of runtime) and is therefore often the default implementation.
Such an implementation has two important properties:
(For a key to be hashable means that we can compute a numeric value from a key which is subsequently used as an index in an array.)
There exist alternative implementations of the dictionary data structure that impose an ordering on the keys – this is often called a sorted dictionary (and is usually implemented in terms of a search tree, though other efficient implementations exist).
To summarize: a dictionary is an ADT that maps keys to values. There are several possible implementations of this ADT, of which the hash table is one. Hash
is a misnomer but in context it’s equivalent to a dictionary that is implemented in terms of a hash table.
"Dictionary" is the name of the concept. A hashtable is a possible implementation.
A dictionary is the collective term given for any data structure implementation used for fast lookups/insertions. This can be achieved/implemented using a variety of data structures like hash table, skip lists, rb tree etc. A hash table is a specific data structure useful for many purposes including implementing a dictionary.
A dictionary uses a key to reference the value directly inside of an associative array.
i.e (KEY => VALUE)
A hash is more often described as a hash table which uses a hash function to calculate the position in memory (or more easily an array) where the value will be. The hash will take the KEY as input and give a value as output. Then plug that value into the memory or array index.
i.e KEY => HASH FUNCTION => VALUE
I guess one is direct while the other isn't. Hash functions may not be perfect either and may sometimes provide an index referencing the wrong value. But that can be corrected.
Best place to look: Wikipedia (associative array and hash table)