转载地址:http://www.cnblogs.com/shenliang123/archive/2012/08/28/2660705.html
--------------------------------------------------------------------------------------------------------------------
java中的遍历
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;/*** map遍历的三种办法* @author us**/ public class test2 {public static void main(String[] args){ /*** new一个map,然后添加内容*/Map map=new HashMap();for (int i = 0; i < 10; i++){map.put(i+"",i+"");System.out.println("添加"+i+"成功");}System.out.println("map大小"+map.size());/*** 1.把值放到一个集合力,然后便利集合*/Collection c=map.values();Iterator it= c.iterator();for (; it.hasNext();){System.out.println(it.next());}/*** 2.把key放到一个集合里,遍历key值同时根据key得到值 (推荐)*/Set set =map.keySet();Iterator it=set.iterator();while(it.hasNext()){String s= (String) it.next();System.out.println(map.get(s));}/*** 3.把一个map对象放到放到entry里,然后根据entry同时得到key和值*/Set set =map.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());}} }