- import java.util.*;
- /*
- 把学生分装成对象,存储到map集合当中
- */
- 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 Demo4
- {
- public static void main(String[] args)
- {
-
-
-
- TreeMap<String,List<Student>> tr = new TreeMap<String,List<Student>>();//这里可不可以使用Set集合?
- List<Student> yure = new ArrayList<Student>(); //两种方式哪个更好一点呢?
- List<Student> jiuye = new ArrayList<Student>();
- tr.put("yurenban",yure);
- tr.put("jiuyeban",jiuye);
- jiuye.add(new Student("zhangsan1","01"));
- jiuye.add(new Student("zhangsan2","02"));
- yure.add(new Student("lishi1","03"));
- yure.add(new Student("lishi2","04"));
- //Set<String> set= tr.keySet();
- Iterator<String> it = tr.keySet().iterator();
- while(it.hasNext())
- {
- String s = it.next();
- List<Student> l = tr.get(s);
- getRome(l);
- }
- }
- public static void getRome(List<Student> list)
- {
- Iterator<Student> it = list.iterator();
- while(it.hasNext())
- {
-
- Student stu = it.next();
- System.out.println(stu);
- }
-
- }
-
- }
复制代码 |