直接看代码吧。老师讲的一个例子。
- import java.util.*;
- class Demo
- {
- public static void main(String [] args)
- {
- duiX();
-
- HashMap<String,HashMap<String,String>> CSDN=new HashMap<String,HashMap<String,String>> ();
- HashMap<String,String> JC=new HashMap<String,String> ();
- HashMap<String,String> JY=new HashMap<String,String> ();
- CSDN.put("基础班",JC);
- CSDN.put("就业班",JY);
- JC.put("416","张三");
- JC.put("445","李四");
- JY.put("332","王五");
- JY.put("329","马六");
- //getInfo(JC); //获取某个教室学生信息。
-
- for (Iterator<String> it=CSDN.keySet().iterator();it.hasNext(); )
- {
- String roomName=it.next();
- HashMap<String,String> room=CSDN.get(roomName);
- System.out.println(roomName+"::");
- getInfo(room); //遍历获取所有班级的学生信息。room会传进所有的班级名。
- }
- }
-
- public static void getInfo(HashMap<String,String> room)
- {
- for (Iterator<String> it=room.keySet().iterator();it.hasNext(); )
- {
- String id=it.next();
- String name=room.get(id);
- System.out.println(id+"++"+name);
- }
- }
-
- public static void getInfo_2(List<Student> list)
- {
- for (Iterator<Student> it=list.iterator();it.hasNext(); )
- {
- Student s=it.next();
- System.out.println(s);
- }
- }
-
- public static void duiX()
- {
- HashMap<String,List<Student>> CSDN=new HashMap<String,List<Student>> ();
- List<Student> JC=new ArrayList<Student> ();
- List<Student> JY=new ArrayList<Student> ();
- CSDN.put("基础班",JC);
- CSDN.put("就业班",JY);
- JC.add(new Student("01","zhangsan"));
- JC.add(new Student("02","lixiaosi"));
- JY.add(new Student("04","wangwu"));
- JY.add(new Student("06","zhaoliu"));
- for (Iterator<String> it=CSDN.keySet().iterator();it.hasNext(); )
- {
- String roomName=it.next();
- List<Student> room=CSDN.get(roomName);
- System.out.println(roomName);
- getInfo_2(room);
- }
- }
- }
- class Student
- {
- private String id;
- private String name;
- Student(String id,String name)
- {
- this.id=id;
- this.name=name;
- }
- public String toString()
- {
- return id+"<>"+name;
- }
- }
复制代码
|
|