When does it make sense to use a card?

I am trying to round up cases where it makes sense to use a map (a set of records with a key). So far I have two categories (see below). Assuming there is more, what are they?

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


Property Values ​​( like a bean )

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

Presence with a capacity of O (1)

 peter -> 1 john -> 1 paul -> 1 
+6
language-agnostic dictionary data-structures mapping map
source share
8 answers

If your language allows you to use both associative arrays and a pointer to functions / procedures, you can use maps to create something similar to object-oriented (see Perl for a classic example).

See here for a more detailed explanation .

+1
source share

Rare data structures (e.g., sparse array or matrix):

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

In addition, I would say that the Presence example you cited is better done using the Set data structure (for example, a HashSet in Java or .NET), since the "display" of a part of the map is really not required.

+5
source share

Recalling function results (caching, buffering, memoization )

 10 -> 2 20 -> 7 30 -> zeroesIn(factorial(30)) 
+3
source share

Conversion

 peter -> pierre john -> jean paul -> paul 
+2
source share

Passing an arbitrary number of optional parameters to a function in a language that does not support them:

 cars = findAvailableCars(make -> 'Toyota', model -> 'Prius', color -> 'green') 
+1
source share

As Eric Petroel said, your “presence” example is better for dialing than for a card.

However, if you want to track 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:

pseudo code:

 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 will just be wordMap["map"]

0
source share

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 nodes to which N is connected.

0
source share

(Thanks for the retag, MatrixFrog.)

Dictionary (mapping a term into a definition)

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

Also in this category

 EADDRINUSE -> "Address in use." EADDRNOTAVAIL -> "Address not available." 
0
source share

All Articles