import java.util.*;
import cn.tianliangedu.jihe.Student2;
public class BianliMap {
public static void main(String[] args) {
test();
}
public static void test()
{
Map<String, Student2> map2 = new HashMap<String, Student2>();
map2.put("小明", new Student2("小明",22));
map2.put("小红", new Student2("小红",98));
map2.put("小杨", new Student2("小杨",95));
System.out.println(map2.get("小明"));
//第一种遍历map的方法,拿到KeySet,遍历keySet
Set<String> keySet = map2.keySet();
for(String arr:keySet)
{
System.out.print("键="+arr+"\t");
System.out.println("值="+map2.get(arr));
}
System.out.println("===================");
//第二种遍历map的方法,拿到entrySet,遍历entrySet(entrySet是键值对)
Set<Map.Entry<String,Student2>> entrySet = map2.entrySet();
for(Map.Entry<String, Student2> es:entrySet)
{
System.out.print("键="+es.getKey()+"\t");
System.out.println("值="+es.getValue());
}
}
}