A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© ifuzhen 中级黑马   /  2014-4-15 01:44  /  1224 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. /*
  2. Map集合被使用是因为具备映射关系。
  3. */

  4. import java.util.*;

  5. class Province
  6. {
  7.         private int id;
  8.         private String name;
  9.         Province(int id,String name)
  10.         {
  11.                 this.id =id;
  12.                 this.name =name;
  13.         }
  14.         public String getInfo()
  15.         {
  16.                 return id+":::"+name;
  17.         }
  18. }
  19. class Test
  20. {
  21.         public static void main(String [] args)
  22.         {
  23.                 HashMap<Integer,List<Province>> hm =new HashMap<Integer,List<Province>>();
  24.                

  25.                 List<Province> CHN =new ArrayList<Province>();
  26.                 List<Province> USA =new ArrayList<Province>();
  27.                 List<Province> ENG =new ArrayList<Province>();
  28.                
  29.                 hm.put(1,CHN);
  30.                 hm.put(2,USA);
  31.                 hm.put(3,ENG);

  32.                 CHN.add(new Province(1,"cd"));
  33.                 CHN.add(new Province(2,"cs"));
  34.                 CHN.add(new Province(3,"cj"));

  35.                 USA.add(new Province(2,"us"));
  36.                 USA.add(new Province(3,"ub"));
  37.                 USA.add(new Province(1,"ua"));

  38.                 ENG.add(new Province(3,"ee"));
  39.                 ENG.add(new Province(1,"ea"));
  40.                 ENG.add(new Province(2,"ez"));

  41.                
  42.                 //entrySet():返回此映射所包含的映射关系的 Set 视图。
  43.                 //Set<Map.Entry<Integer,String>> me =hm.entrySet();  Set<Map.Entry<K,V>>
  44.                 /*for (Iterator<Map.Entry<Integer,List<Province>>> it =hm.entrySet().iterator();it.hasNext() ; )
  45.                 {
  46.                         Map.Entry<Integer,List<Province>> me =it.next();
  47.                         Integer key=me.getKey();
  48.                         List<Province> value =me.getValue();
  49.                        
  50.                         sop(getCountryInfo(value));
  51.                 }

  52.                 //keySet();返回此映射中所包含的键的 Set 视图。         Set<K>

  53.                 /*for (Iterator<Integer> it =hm.keySet().iterator(); it.hasNext(); )
  54.                 {
  55.                         Integer key =it.next();
  56.                         String value =hm.get(key);
  57.                         sop(key+"::::"+value);
  58.                 }

  59.                 */

  60.                 for (Iterator<Integer> it =hm.keySet().iterator(); it.hasNext(); )
  61.                 {
  62.                         Integer key =it.next();
  63.                         List<Province> value=hm.get(key);
  64.                         sop(getCountryInfo(value));
  65.                 }

  66.                
  67.         }

  68.         public static String getCountryInfo(List<Province> value)
  69.         {
  70.                 StringBuffer sb =new StringBuffer();
  71.                 Iterator<Province> it =value.iterator();
  72.                 for (;it.hasNext() ; )
  73.                 {
  74.                         Province p =it.next();
  75.                         sb.append(p.getInfo()+"\t");
  76.                 }

  77.                 return sb.toString();
  78.         }
  79.         public static void sop(Object obj)
  80.         {
  81.                 System.out.println(obj);
  82.         }
  83. }
复制代码

1 个回复

倒序浏览
值得学习ing!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马