Traverse a Map

From CodeCodex

Revision as of 13:16, 25 June 2007 by Jdh30 (Talk | contribs)

Implementations

F#

Map.fold

Java

This code assumes that TheMap is already instantiated. It will loop over TheMap and will list the name values pairs contained within the map. <HIGHLIGHTSYNTAX language="java122">

     Set keys = TheMap.keySet();         // The set of keys in the map.
     Iterator keyIter = keys.iterator();
     System.out.println("The map contains the following associations:");
     while (keyIter.hasNext()) {
        Object key = keyIter.next();  // Get the next key.
        Object value = TheMap.get(key);  // Get the value for that key.
        System.out.println( "   (" + key + "," + value + ")" );
     }

</HIGHLIGHTSYNTAX>

OCaml

MyMap.fold

Python

<HIGHLIGHTSYNTAX language="python">

   sys.stdout.write("The map contains the following associations:\n")
   for key in TheMap.iterkeys() :
       sys.stdout.write \
         (
               "    (%s,%s)\n"
           %
               (str(key), str(TheMap[key]))
         )
   #end for

</HIGHLIGHTSYNTAX>