Show how to iterate over the key-value pairs of an associative array, and print each pair out.

Also show how to iterate just over the keys, or the values, if there is a separate way to do that in your language.



See also

 Array
 Associative array: Creation, Iteration
 Collections
 Compound data type
 Doubly-linked list: Definition, Element definition, Element insertion, List Traversal, Element Removal
 Linked list
 Queue: Definition, Usage
 Set
 Singly-linked list: Element definition, Element insertion, List Traversal, Element Removal
 Stack


 
using Gee;
 
void main(){
    // declare HashMap                                                          
    var map = new HashMap<string, double?>();
 
    // set 3 entries                                                            
    map["pi"] = 3.14;
    map["e"] = 2.72;
    map["golden"] = 1.62;
 
    // iterate over (key,value) pair                                            
    foreach (var elem in map.entries){
        string name = elem.key;
        double num = elem.value;
 
	stdout.printf("%s,%f\n", name, num);
    }
 
    // iterate over keys                                                        
    foreach (string key in map.keys){
	stdout.printf("%s\n", key);
    }
 
    // iterate over values                                                      
    foreach (double num in map.values){
	stdout.printf("%f\n", num);
    }
}
 