一个学校多个教室的例子
import java.util.*;
class Student
{
private String id;
private String name;
Student(String name,String id)
{
this.id=id;
this.name=name;
}
public String toString()
{
return id+"..."+name;
}
}
class MapDemo3
{
public static void main(String[] args)
{
demo();
}
public static void demo()
{
HashMap<String ,List<Student>> hm=new HashMap<String,List<Student>>();
List<Student> yure=new ArrayList<Student>();
List<Student> jiuye=new ArrayList<Student>();
hm.put("yureban",yure);
hm.put("jiuyeban",jiuye);
yure.add(new Student("zhangsan01","21"));
yure.add(new Student("zhangsan02","22"));
jiuye.add(new Student("zhangsan03","23"));
jiuye.add(new Student("zhangsan04","24"));
Iterator<String> it=hm.keySet().iterator();//这句是不是就调用了keyset方法了?
while (it.hasNext())
{
String roomName=it.next();
List<Student> room=hm.get(roomName);
System.out.println(roomName);
getInfos(room);
}
}
public static void getInfos(List<Student> list)
{
Iterator<Student> it1=list.iterator();
while (it1.hasNext())
{
Student s=it1.next();
System.out.println(s);//从打印结果来这两句调用了toString,但是具体是怎么回事还是不太了解,
//还有没有其他的方法
}
}
}
|