cn.jdbc.test Package;
Import the java.util.HashMap;
Import the java.util.Iterator;
Import a java.util.Map;
Import java.util.Map.Entry;
/ **
* Map collection cycle traversal
* @data 2018.1 .21
*
* /
public class TestMap {
public static void main (String [] args) {
the Map <String, Object> = new new the HashMap Map <String, Object> ();
map.put ( "AAA", 111);
Map .put ( "BBB", 222);
map.put ( "CCC", 333);
map.put ( "ddd", 444);
// set the Map mode loops through a
System.out.println ( "first : traversing through map.keySet () key and value: ");
for (String key: map.keySet () ) {// keySet acquiring map set key set key can then traverse the
String value = as map.get (key) .toString (); //
System.out.println ( " key: "+ key +" vlaue: "+ value);
}
// set the Map by the iterator loop through two way
System.out.println (" second: Map.entrySet traversal key and value by using the iterator: ") ;
the Iterator <the entry <String, Object >> EnumMap.entrySet IT = () Iterator ();.
the while (it.hasNext ()) {
the entry <String, Object> = it.next entry ();
System.out.println ( "Key:" entry.getKey + () + "Key:" entry.getValue + ());
}
// set the Map three recommended traversal cycle, especially when a large-capacity
System.out.println ( "third : The Map.entrySet traverse key and value ");
for (of Map.Entry <String, Object> m: EnumMap.entrySet ()) {
System.out.println ( "Key:" m.getKey + () + "value:" + m.getValue ());
}
/ / fourth:
System.out.println ( "fourth: through all value by Map.values (), but not traversing Key");
for (m Object: map.values ()) {
the System.out. the println (m);
}
}
}
Map ways to loop through the collection of
Guess you like
Origin www.cnblogs.com/zhuyeshen/p/11599245.html
Recommended
Ranking