使用HashMap存储多个企鹅信息,然后统一使用Iterator进行遍历
package work;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class Test15 {
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妹");
Set<String> keys = map.keySet();
Iterator it = keys.iterator();
System.out.println("===============================");
while (it.hasNext()) {
String key = (String) it.next();
System.out.println(" " + key + ": " + map.get(key));
}
System.out.println("===============================");
HashMap<Integer, String> map1 = new HashMap<Integer, String>();
map1.put(1, "哈哈Q仔");
map1.put(2, "练练Q仔");
map1.put(3, "皮皮Q仔");
map1.put(4, "啦啦Q妹");
Set<Integer> keys1 = map1.keySet();
Iterator<Integer> it1 = keys1.iterator();
System.out.println("使用Iterate 进行遍历");
while (it1.hasNext()) {
Integer key1 = it1.next();
System.out.println(" " + key1 + ": " + map1.get(key1));
}
System.out.println();
System.out.println("使用foreach进行遍历");
for (Integer integer : keys1) {
System.out.println(" " + integer + ": " + map1.get(integer));
}
}
}