views:

479

answers:

12

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?

A: 

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.

Geo
+14  A: 

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:

definition['pernicious']

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.

Chad Birch
That's what I was going to say! You beat me to it
Andy White
Really, the point of the hash is to make looking up the object faster. If that hashed object is associated with another object, that's great (and probably the most common situation). But it isn't necessary: Consider hash_set in C++ ( http://www.sgi.com/tech/stl/hash_set.html )
Brian
I don't think this is a true real-world example. A human looking through a dictionary will use an interpolation search. Physical dictionaries don't have a O(1) way of find a word.
Ben S
He didn't ask for a real-world ANALOG of a hash, he asked for an example of something that you'd represent with one.
Chad Birch
@Chad: Yes, and one was given: a dictionary.
thenduks
+5  A: 

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).

Brian
+1  A: 

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"

etc

temp2290
+2  A: 

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).

Alex B
A: 

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).

dwc
+1  A: 

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.

RnR
A: 

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.

pearcewg
A: 

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

anil
A: 

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.

sth
A: 

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.

Dining Philanderer
A: 

(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:

$users["manager"]
elcuco