第十五周作业

使用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("\t" + key + ":\t" + 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("\t" + key1 + ":\t" + map1.get(key1));
         }
System.out.println();
System.out.println("使用foreach进行遍历");
for (Integer integer : keys1) {
System.out.println("\t" + integer + ":\t" + map1.get(integer));
           }

       }

}

posted @ 2020-06-16 12:20  修祈  阅读(122)  评论(0编辑  收藏  举报