代码最后获取学生信息,迭代器是如何获取的呢?
如果在学生对象中定义获取方法get( );还好理解,
但这里直接覆盖toString(),程序会是自动调用toString()么?
请各位帮忙看看
import java.util.*;
class Student
{
private String name;
private String id;
Student(String id,String name)
{
this.name = name;
this.id = id;
}
public String toString()
{
return id+"...."+name;
}
}
class MapDemo5
{
public static void main(String[] args)
{
HashMap<String,List<Student>> bbxy = new HashMap<String,List<Student>>();
List<Student> yure = new ArrayList<Student>();
List<Student> jiuye = new ArrayList<Student>();
bbxy.put("yureban",yure);
bbxy.put("jiuyeban",jiuye);
yure.add(new Student("01","xushuia"));
yure.add(new Student("02","huia"));
yure.add(new Student("03","ushuia"));
jiuye.add(new Student("01","ahrt"));
jiuye.add(new Student("02","haha"));
jiuye.add(new Student("03","cahe"));
jiuye.add(new Student("04","gdfah"));
//遍历bbxy集合,获取所有的数据
Iterator<String> it = bbxy.keySet().iterator();
while(it.hasNext())
{
String roomName = it.next();
List<Student> room = bbxy.get(roomName);
System.out.println(roomName);
getStu(room);
}
}
public static void getStu(List<Student> list)
{
Iterator<Student> it = list.iterator();
while (it.hasNext())
{
Student s = it.next();
System.out.println(s);
}
}
}
|