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.
Software Engineering Stack Exchange is a question and answer site for professionals, academics, and students working within the systems development life cycle. It only takes a minute to sign up.
Sign up to join this communityHash
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.
unordered_map
to show what they do rather than what they are.
Dec 28, 2010 at 16:10
Hash
class with a hash table, since Ruby 1.9 Hash
es preserve the insertion order while a hash table does not. So, in Ruby 1.9, the name Hash
doesn't even reflect the implementation any more.
May 21, 2012 at 11:46
"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)