Cyclic traversal of map
The code is as follows: |
Copy code |
Package com. sec. map; Import java. util. HashMap; Import java. util. Iterator; Import java. util. Map; Public class TestMap { Public static void main (String [] args ){ Map <String, String> map = new HashMap <String, String> (); Map. put ("1", "value1 "); Map. put ("2", "value2 "); Map. put ("3", "value3 "); // The first type is commonly used. The second value is used. System. out. println ("traverse key and value through Map. keySet :"); For (String key: map. keySet ()){ System. out. println ("key =" + key + "and value =" + map. get (key )); } System. out. println ("www.111cn.net "); // Type 2 System. out. println ("traverse key and value through Map. entrySet using iterator :"); Iterator <Map. Entry <String, String> it = map. entrySet (). iterator (); While (it. hasNext ()){ Map. Entry <String, String> entry = it. next (); System. out. println ("key =" + entry. getKey () + "and value =" + entry. getValue ()); } System. out. println ("www.111cn.net "); // Method 3: recommended, especially when the capacity is large System. out. println ("traversing key and value through Map. entrySet "); For (Map. Entry <String, String> entry: map. entrySet ()){ System. out. println ("key =" + entry. getKey () + "and value =" + entry. getValue ()); } System. out. println ("www.111cn.net "); // Category 4 System. out. println ("traverse all values through Map. values (), but not key "); For (String v: map. values ()){ System. out. println ("value =" + v ); } System. out. println ("www.111cn.net "); } } |
Supplement:
First, use the for loop
Java code
The code is as follows: |
Copy code |
For (Map. Entry <String, String> entry: map. entrySet ()){ System. out. println (entry. getKey () + "--->" + entry. getValue ()); } For (Map. Entry <String, String> entry: map. entrySet ()){ System. out. println (entry. getKey () + "--->" + entry. getValue ()); } |
Second, iteration
Java code
The code is as follows: |
Copy code |
Set set = map. entrySet (); Iterator I = set. iterator (); While (I. hasNext ()){ Map. Entry <String, String> entry1 = (Map. Entry <String, String>) I. next (); System. out. println (entry1.getKey () + "=" + entry1.getValue ()); } Set set = map. entrySet (); Iterator I = set. iterator (); While (I. hasNext ()){ Map. Entry <String, String> entry1 = (Map. Entry <String, String>) I. next (); System. out. println (entry1.getKey () + "=" + entry1.getValue ()); }
|
Use keySet () for iteration
Java code
The code is as follows: |
Copy code |
Iterator it = map. keySet (). iterator (); While (it. hasNext ()){ String key; String value; Key = it. next (). toString (); Value = map. get (key ); System. out. println (key + "--" + value ); } Iterator it = map. keySet (). iterator (); While (it. hasNext ()){ String key; String value; Key = it. next (). toString (); Value = map. get (key ); System. out. println (key + "--" + value ); } |
Use entrySet () for iteration
Java code
The code is as follows: |
Copy code |
Iterator it = map. entrySet (). iterator (); System. out. println (map. entrySet (). size ()); String key; String value; While (it. hasNext ()){ Map. Entry entry = (Map. Entry) it. next (); Key = entry. getKey (). toString (); Value = entry. getValue (). toString (); System. out. println (key + "=" + value ); } |