See More

package first; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import org.junit.Test; public class ForDemo { @Test//Map¼¯ºÏµÄµÚÒ»ÖÖÈ¡³öÔªËØ·½Ê½ public void test1(){ //LinkedHashMapÊÇÓöÓÁÐÀ´ÊµÏÖµÄ //HashMapÊÇÓÃHashËã·¨À´´æ´¢ÔªËØ Map map=new LinkedHashMap(); map.put("1","uu"); map.put("2","zhouyou"); map.put("3", "uchiyou"); Set keys=map.keySet(); Iterator it=keys.iterator(); while(it.hasNext()){ String key=it.next(); System.out.println(key+" = "+map.get(key)); } } //map¼¯ºÏµÄµÚ¶þÖÖÔªËØÈ¡³ö·½Ê½ @Test public void test2(){ Map map=new HashMap(); map.put(1,"xiaoqiang"); map.put(2, "qangcai"); map.put(3, "value"); Set> entry=map.entrySet(); Iterator> it=entry.iterator(); while(it.hasNext()){ Map.Entry e=it.next(); System.out.println(e.getKey()+" "+e.getValue()); } } }