package Ac; import java.util.*; public class Test { public static void main(String[] args) { // TODO Auto-generated method stub show(); } public static void show() { Map a = new HashMap(); a.put("欧欧","Q仔"); a.put("亚亚","Q妹"); a.put("菲菲","Q妹"); a.put("美美","Q妹"); System.out.println("共计有"+a.size()+"只企鹅"); Set keys = a.keySet(); Iterator it = keys.iterator(); System.out.println("分别是:"); while (it.hasNext()) { String key = (String)it.next(); String b = (String) a.get(key); System.out.println(key+" "+b); } } }
package Ac; import java.util.*; public class Test { public static void main(String[] args) { // TODO Auto-generated method stub show(); } public static void show() { Map<String,String> a = new HashMap<String,String>(); a.put("欧欧","Q仔"); a.put("亚亚","Q妹"); a.put("菲菲","Q妹"); a.put("美美","Q妹"); System.out.println("共计有"+a.size()+"只企鹅"); System.out.println("iterator输出"); //iterator输出 Set<String> keys = a.keySet(); Iterator<String> it = keys.iterator(); System.out.println("分别是:"); while (it.hasNext()) { String key = (String)it.next(); String b = (String) a.get(key); System.out.println(key+" "+b); } System.out.println("foreach语句输出"); //foreach语句输出 for (String key : keys) { String b = a.get(key); System.out.println(key+" "+b); } } }