Maps - Associative Data

A "mapping" is a relationship between two entities. For example, the phone book is a mapping between names (type == string) and numbers (type==number). Arrays are a very basic "mapping" between a number and a value. Often the only relationship in an array is the "bucket index" that the data happens to be placed in. In a MAP, there is a much more defined notion of this relationship.

Maps

Matlab provides the use of the JAVA language for advanced programming techniques. For our purposes, we will simply "use" predefined Java "data types". The one we are interested in is the "map" or "hashtable".

A Map in computer terms is a relationship (or association) between one "value" and another "value". The Map is an advanced data type that allows you to assign a value (the data to be "linked" (or assocaited with)) with a "key" (the "value" used to look up the data). The key is often a string, but could be any type of data.

Map/Hashtable Terms

  1. Key: The value that is used to look up another value. For example, in a phone book, the key is the name and the value is the phone number:

  2. Value: The data associated with each key. Every key that has been entered into a hash table must have one (and only one) value associated with it.

Map Syntax

To create a Map in Matlab, we use the following syntax, which assigns a Map data type into the variable, 'map'.:

        
        map = java.util.Hashtable;
        
      

To use a map, we use the following syntax: we "put" things into the map. or we "get" things out of the map.

  1. Putting Data into a Map

    To put a value into the map, associating a word with a value we use:

              
    
            map.put( word, value );
    
            % this is equivalent to the Matlab array notation:
            % map[word] = value;
            % but must be written in the above form.
              
            

    If the word already existed in our relationship, the old value is removed and the new value used instead.

  2. Getting Data out of a Map

    To get the value out of the map, the value associate with a given word, we use:

              
            variable = map.get( word );                  
    
            % this is equivalent to the Matlab array notation:
            % variable = map[word];
            % but must be written in the above form.
              
            

    If the word does not exist in our relationship (in our map) the result of map.get( word ) will be an empty array.

Example of creating a phone book

Say we wanted to create a phone book using a Map. We could do it in the following maneer:

Below, we store each person's phone number as being "associated" with their name:

        
            phone_book = java.util.Hashtable;

            phone_book.put('jim', 5055220211);
            phone_book.put('evrard', 8011234567);
            phone_book.put('murali', 1234567890);
            phone_book.put('sankadip', 9879991234);

        
      

Now, to use the data, we only have to "ask" the map to give us the information back.

        

            % here we show how to check if a person
            % is in the map before printing the value.
            
            phone_number    = phone_book.get('jane');

            if ( isempty(phone_number) )
              fprintf('jane does not have a phone\n');
            else
              fprintf('jane''s phone number is %d\n', phone_number);
            end
        
      

Analogy : Arrays

Arrays can be considered Map that associate a number (the index into the array) and a value (the data at that location in the array). Hence the Key is the number, and the Data is the value.

Unfortunately, the numbers you can use as "Keys" are restricted ot 1,2,3,4,... etc. and every number must be used.

The general mechanics are the same. We "put" information into an array and we "get" information out of the array.

        
            line_position_map = java.util.Hashtable;

            % here we create a "mapping" between a number and a string
            % The Array syntax and map syntax are shown side by side:
            
            line_position_array{1} = 'jim';    line_position_map.put(1,'jim');
            line_position_array{2} = 'jane';   line_position_map.put(2,'jane');
            line_position_array{3} = 'janet';  line_position_map.put(3,'janet');

            % here we get the person who is first in line
            % The Array syntax and map syntax are shown side by side:

            first_in_line = line_position_array{1};
            first_in_line = line_position_map.get(1);

        
      

Reverse Phone Book Example

Lets create a mapping that is the reverse of a phone book, in other words from the phone number back to the name.

        
            reverse_phone_book = java.util.Hashtable;

            reverse_phone_book.put(5051231234,'jim');
            reverse_phone_book.put(8011231234,'jane');
            reverse_phone_book.put(9991234567,'janet');

            % here we relate a phone number with a name.
            phone_number = input('what is the person''s phone number? ');

            name = reverse_phone_book.get( phone_number );
            
            if ( isempty(name) )
              fprintf('we don''t have a number %d in our phone book\n', phone_number);
            else
              fprintf('%s''s phone number is %d\n', name, phone_number);
            end
        
      


Back to Topics List