In Java, the list set is required in part of the time. The map set is occasionally used, but it is often forgotten how to traverse the map set. Today, we record the following:
Java Traversal
Import Java. util. collection; import Java. util. hashmap; import Java. util. iterator; import Java. util. list; import Java. util. map; import Java. util. set; import Java. util. map. entry;/*** three methods for map traversal * @ author us **/public class Test2 {public static void main (string [] ARGs) {/*** a new map, and then add the content */map = new hashmap (); For (INT I = 0; I <10; I ++) {map. put (I + "", I + ""); system. out. println ("added" + I + "succeeded");} system. out. println ("map size" + map. size ();/*** 1. put the value in a set combination, and then facilitate the Set */collection C = map. values (); iterator it = C. iterator (); For (; it. hasnext ();) {system. out. println (it. next ();}/*** 2. put the key in a set, traverse the key value and obtain the value (recommended) based on the key */set = map. keyset (); iterator it = set. iterator (); While (it. hasnext () {string S = (string) it. next (); system. out. println (map. get (s);}/*** 3. put a map object in the entry, and then obtain the key and value */set = map at the same time according to the entry. entryset (); iterator it = set. iterator (); While (it. hasnext () {map. entry <string, string> entry = (Entry <string, string>) it. next (); system. out. println (entry. getkey () + ":" + entry. getvalue ());}}}
Traversal of the map set in freemarker:
<Table width = "300" border = 1> <thead> <tr> <TH width = "100"> source mode </Th> <TH width = "120"> Number of Students </Th> </tr> </thead> <tbody> <# If clientsourcedata? Exists> <# list clientsourcedata? Keys as key> <tr> <TD >$ {key} </TD> <TD >$ {clientsourcedata. get (key) }</TD> </tr> </# list> </# If> <tr> <TD> total: </TD> <TD >$ {totalnum} </TD> </tr> </tbody> </table>