Map traversal in Java and freemarker

Source: Internet
Author: User

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>

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.