package aaa; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class Text { public static void main(String[] args) { Map<String, String> map = new HashMap<String, String>(); map.put("露露", "Q妹"); map.put("菲菲", "Q妹"); map.put("欧欧", "Q仔"); map.put("亚亚", "Q妹"); map.put("美美", "Q妹"); System.out.println("一共有"+map.size()+"个企鹅,使用Iterator遍历,所有企鹅的昵称和品种分别是:"); Set<String> keyset = map.keySet(); Iterator<String> it = keyset.iterator(); while (it.hasNext()) { String x = it.next(); String s = map.get(x); System.out.println(x + " " + s); } System.out.println("一共有"+map.size()+"个企鹅,使用foreach遍历,所有企鹅的昵称和品种分别是:"); for (String a : map.keySet()) { String b = map.get(a); System.out.println(a + " " + b); } } }
package aaa; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class Text { public static void main(String[] args) { Map<String, String> map = new HashMap<String, String>(); map.put("露露", "Q妹"); map.put("菲菲", "Q妹"); map.put("欧欧", "Q仔"); map.put("亚亚", "Q妹"); map.put("美美", "Q妹"); System.out.println("一共有" + map.size() + "个企鹅,所有企鹅的昵称和品种分别是:"); Set<String> keyset = map.keySet(); Iterator<String> it = keyset.iterator(); while (it.hasNext()) { String x = it.next(); String s = map.get(x); System.out.println(x + " " + s); } } }