views:

144

answers:

8

I am trying to round up cases when it makes sense to use a map (set of key-value entries). So far I have two categories (see below). Assuming more exist, what are they?

Please limit each answer to one unique category and put up an example.


Property values (like a bean)

age -> 30
sex -> male
loc -> calgary   

Presence, with O(1) performance

peter -> 1
john  -> 1
paul  -> 1
+5  A: 

Sparse Data Structures (e.g. a sparse array or a matrix):

0 -> value
1 -> value
100 -> value
105 -> value

Also, I would argue that the "Presence" example you listed is better done with a Set data structure (e.g. HashSet in Java or .NET) since the "mapping" part of the map is really not necessary.

Eric Petroelje
+3  A: 

Remembering function results (caching, buffering, memoization)

10 -> 2
20 -> 7
30 -> zeroesIn(factorial(30))
kiwicptn
A: 

As Eric Petroelje said, your "presence" example is better suited to a Set than a Map.

However, if you want to keep track of the number of occurrences of things, use a Map. For example, you want to know how many times a given word appears in a document:

pseudocode:

wordMap = map()
for word in document:
    if wordMap.containsKey(word):
        wordMap[word]++
    else:
        wordMap[word] = 1

then if I want to know how many times the word 'map' appears in the document, it would just be wordMap["map"]

MatrixFrog
I'd regroup those under "histograms", e.g. number of occurences for word, score received for test, etc.
kiwicptn
A: 

A Map is one way of representing a graph. The keys are the nodes in the graph, and the value for a particular node N is a list of all the nodes that N connects to.

MatrixFrog
`The value ... is a list.` When you start thinking like that (also `the value is a map`) the scope of the question explodes. I would like to keep this simple with scalar (or zero-dimension) values.
kiwicptn
+2  A: 

Conversion

peter -> pierre
john  -> jean
paul  -> paul
kiwicptn
+1  A: 

If your language allows both associative arrays and pointer to fuctions/procedures, you can use maps to build something similar to Object Oriented (see Perl for a classical example).

See here for a more detailed explanation.

p.marino
Not sure I quite get it but it's interesting. Can you provide an example?
kiwicptn
If you look at how Perl added OO elements to the language you will find plenty of info. Basically if you can use a "method signature" as a key and a "function pointer" as a value, you can use an hashmap to add "Object Oriented behaviour".
p.marino
So a method signature (basically a string) maps to a function like `paul -> dealWithPaul()`. Makes me think of the factory method: `pizza -> bakePizza()`. Good +1.
kiwicptn
It also allow you to define properties using topping->(list of topping ingredients) for example. Or price->number.
p.marino
@kiwicptn - If you think of how C++ for example uses a virtual function table to determine which method to call (base or derived) then I think that is what he is getting at here. A base class sets up a mapping of signatures to its methods and a derived class can replace those mappings with its own methods, changing the behavior of the class.
Eric Petroelje
Selected as the accepted (most fascinating) answer.
kiwicptn
A: 

(Thanks for the retag, MatrixFrog.)

Dictionary (mapping a term to a definition)

"postulate" -> "demand or claim"
"consulate" -> "residence of a foreign official"

Also in this category

EADDRINUSE    -> "Address in use." 
EADDRNOTAVAIL -> "Address not available."
kiwicptn
+1  A: 

Passing arbitrary number of optional parameters to a function, in a language which doesn't support them:

cars = findAvailableCars(make -> 'Toyota', model -> 'Prius', color -> 'green')
Mladen Jablanović