HashMap的两种取出方式

作者: 李多多 日期: 2019-03-27

HashMap的两种取出方式
public class Demo{
public static void main(String[] args){
Map<Student,String> map = new HashMap<Student,String>();
map.put(new Student("Kevin",21),"重庆");
map.put(new Student("Litch",21),"四川");
map.put(new Student("Rocco",24),"江西");
map.put(new Student("Lenka",21),"广东");
//----------第一种keySet
Set<Student> set = map.keySet();
Iterator<Student> it = set.iterator();
while(it.hasNext()){
System.out.println(map.get(it.next()));
}
//-----------第二种取出方式entrySet
Set<Map.Entry<Student,String>> s = map.entrySet();
Iterator<Entry<Student, String>> its = s.iterator();
while(its.hasNext()){
Map.Entry<Student, String> ma = (Map.Entry<Student, String>)its.next();
System.out.println(ma.getKey()+"--"+ma.getValue());
}
}
}