Chinaunix首页 | 论坛 | 博客
  • 博客访问: 258252
  • 博文数量: 164
  • 博客积分: 60
  • 博客等级: 民兵
  • 技术积分: 1129
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-09 21:55
文章分类

全部博文(164)

文章存档

2017年(2)

2015年(67)

2014年(95)

我的朋友

分类: Java

2014-05-17 13:43:30




点击(此处)折叠或打开

  1. /*
  2. map扩展知识。

  3. map集合被使用是因为具备映射关系。

  4. "yureban" Student("01" "zhangsan");

  5. "yureban" Student("02" "lisi");

  6. "jiuyeban" "01" "wangwu";
  7. "jiuyeban" "02" "zhaoliu";

  8. 一个学校有多个教室。每一个教室都有名称。


  9. */
  10. import java.util.*;

  11. class Student
  12. {
  13.     private String id;
  14.     private String name;
  15.     Student(String id,String name)
  16.     {
  17.         this.id = id;
  18.         this.name = name;
  19.     }
  20.     public String toString()
  21.     {
  22.         return id+":::"+name;
  23.     }
  24. }
  25. class MapDemo3
  26. {

  27.     public static void demo()
  28.     {
  29.         HashMap<String,List<Student>> czbk = new HashMap<String,List<Student>>();

  30.         List<Student> reyu = new ArrayList<Student>();
  31.         List<Student> jiuye = new ArrayList<Student>();

  32.         czbk.put("yureban",reyu);
  33.         czbk.put("jiuyeban",jiuye);

  34.         reyu.add(new Student("01","zhagnsa"));
  35.         reyu.add(new Student("04","wangwu"));
  36.         jiuye.add(new Student("01","zhouqi"));
  37.         jiuye.add(new Student("02","zhaoli"));


  38.         Iterator<String> it = czbk.keySet().iterator();

  39.         while(it.hasNext())
  40.         {
  41.             String roomName = it.next();
  42.             List<Student> room = czbk.get(roomName);
  43.             
  44.             System.out.println(roomName);
  45.             getInfos(room);
  46.         }

  47.         

  48.     
  49.     }
  50.     public static void getInfos(List<Student> list)
  51.     {
  52.         Iterator<Student> it = list.iterator();
  53.         while(it.hasNext())
  54.         {
  55.             Student s = it.next();
  56.             System.out.println(s);
  57.         }
  58.     }




  59.     public static void main(String[] args)
  60.     {
  61.          demo();
  62.         
  63.         /*
  64.         HashMap> czbk = new HashMap>();

  65.         HashMap yure = new HashMap();
  66.         
  67.         HashMap jiuye = new HashMap();

  68.         czbk.put("yureban",yure);
  69.         czbk.put("jiuyeban",jiuye);


  70.         yure.put("01","zhagnsan");
  71.         yure.put("02","lisi");

  72.         jiuye.put("01","zhaoliu");
  73.         jiuye.put("02","wangwu");



  74.         //遍历czbk集合。获取所有的教室。
  75.         Iterator it = czbk.keySet().iterator();

  76.         while(it.hasNext())
  77.         {
  78.             String roomName = it.next();
  79.             HashMap room = czbk.get(roomName);
  80.             
  81.             System.out.println(roomName);
  82.             getStudentInfo(room);
  83.         }

  84.         
  85. //        getStudentInfo(jiuye);
  86. //        getStudentInfo(yure);
  87. */
  88.         
  89.     }
  90.     public static void getStudentInfo(HashMap<String,String> roomMap)
  91.     {
  92.         Iterator<String> it = roomMap.keySet().iterator();

  93.         while(it.hasNext())
  94.         {
  95.             String id = it.next();
  96.             String name = roomMap.get(id);
  97.             System.out.println(id+":"+name);
  98.         }
  99.     }
  100. }

阅读(476) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~