Tutorial :What's a real world example of something you would represent with a hash?


I'm just trying to get a grip on when you would need to use a hash and when it might be better to use an array. What kind of real-world object would a hash represent, say, in the case of strings?


I believe sometimes a hash is referred to as a "dictionary", and I think that's a good example in itself. If you want to look up the definition of a word, it's nice to just do something like:


Instead of trying to figure out the correct numeric index that the definition would be stored at.

This answer assumes that by "hash" you're basically just referring to an associative array.


I think you're looking at things in the wrong direction. It is not the object which determines if you should use a hash but the manner in which you are accessing it. A common use of a hash is when using a lookup table. If your objects are strings and you want to check if they exist in a Dictionary, looking them up will (assuming the hash works properly) by O(1). WIth sorting, the time would instead be O(logn), which may not be acceptable.

Thus, hashes are ideal for use with Dictionaries (hashmaps), sets (hashsets), etc.

They are also a useful way of representing an object without storing the object itself (for passwords).


The phone book - key = name, value = phone number.

I also think of the old World Book Encyclopedias (actual books). Each article is "hashed" into a single book (cat goes in the "C" volume).


Any time you have data that is well served by a 1-to-1 map.

For example, grades in a class:

"John Smith" => "B+"

"Jacob Jenkens" => "C"



In general hashes are used to find things fast - a hash map can be used to assosiate one thing with another fast, a hash set will just store things "fast".

Please consider also the hash function complexity and cost when considering whether it's better to use a hash container or a normal less then container - the additional size of the hash value and the time needed to compute a "perfect" hash, and the time needed to make a 1:1 comparision at the end in case of a hash function conflict may in fact be a lot higher then just going through a tree structure with logharitmic complexity using the less then operators.


When you need to associate one variable with another. There isn't a "type limit" to what can be a key/value in a hash.


Hashed have many uses. Aside from cryptographic uses, they are commonly used for quick lookups of information. To get similarly quick lookups using an array you would need to keep the array sorted and then used a binary search. With a hash you get the fast lookup without having to sort. This is the reason most scripting languages implement hashing under one name or another (dictionaries, et al).


I use one often for a "dictionary" of settings for my app.

Setting | Value

I load them from the database or config file, into hashtable for use by my app.

Works well, and is simple.


One example could be zip code associated with an area, city or any postal address.


A good example is a cache with lot's of elements in it. You have some identifer by which you want to look up the a value (say an URL, and you want to find the according cached webpage). You want these lookups to be as fast as possible and don't want to search through all the stored pages everytime some URL is requested. A hash table is a great data structure for a problem like this.


One real world example I just wrote is when I was adding up the amount people spent on meals when filing expense reports.

I needed to get a daily total with no idea how many items would exist on a particular day and no idea what the date range for the expense report would be. There are restrictions on how much a person can expense with many variables (What city, weekend, etc...)

The hash table was the perfect tool to handle this. The key was the date the value was the receipt amount (converted to USD). The receipts could come in in any order, i just keep getting the value for that date and adding to it until the job was done. Displaying was easy as well.


(php code)

$david        = new stdclass();  $david->name  = "david";  $david->age   = 12;  $david->id    = 1;  $david->title = "manager";    $joe        = new stdclass();  $joe->name  = "joe";  $joe->age   = 17;  $joe->id    = 2;  $joe->title = "employee";    // option 1: lets put users by index  $users[] = $david;  $users[] = $joe;    // option 2: lets put users by title  $users[$david->title] = $david;  $users[$joe->title]   = $joe;  

now the question: who is the manager? answer:


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »