- import java.util.*;
- class Student
- {
- private String name;
- private String id;
- Student(String name,String id)
- {
- this.name = name;
- this.id = id;
- }
- public String toString()
- {
- return name+";;;;;;;"+id;
- }
- }
- class Demo
- {
- public static void main(String[] args)
- {
- Map<String,List<Student>> map= new TreeMap<String,List<Student>>();
- List<Student> yure = new ArrayList<Student>();
- List<Student> jiuye = new ArrayList<Student>();
- map.put("jiuyeban",jiuye);
- map.put("yureban",yure);
- yure.add(new Student("zhangsan","01")); //既然是学生对象的话,可不可以这样添加呢?
- //现在外面把 学生对象建立好在往里面添加
- yure.add(new Student("lisi","02")); //是不是也可以呢?这两种方式没有什么不同的吧?
- jiuye.add(new Student("wangwu","03")); //哪一种方式比较节省空间呢?
- jiuye.add(new Student("zhaoliu","04"));
- Set<String> set= map.keySet();
- Iterator<String> it1= set.iterator();
- while(it1.hasNext())
- {
- String s1= it1.next();
- List<Student> rom= map.get(s1);
- System.out.println(s1);
- sop(rom);
- }
-
- }
- public static void sop(List<Student> list)
- {
- Iterator<Student> it= list.iterator();
- while(it.hasNext())
- {
- Student s= it.next();
- System.out.println(s);
- }
- }
- }
复制代码 |