Java,J2EE,Weblogic,Oracle

          java項目隨筆
          隨筆 - 90, 文章 - 6, 評論 - 61, 引用 - 0
          數據加載中……

          遍歷HashMap

          Example 1: 通過迭代key得到value中的值

           

          try {

          HashMap hm = new HashMap();

          hm.put("1", "yi");

          hm.put("2", "er");

          hm.put("3", "san");

          hm.put("4", "si");

          hm.put("5", "wu");

          Iterator it=hm.keySet().iterator();

          while(it.hasNext()){

          String key=(String)it.next();

          System.out.println(hm.get(key));

          }

          } catch (Exception e) {

          e.printStackTrace();

           

          }

           

          Example 2:直接迭代value中的值

           

          try {

          HashMap hm = new HashMap();

          hm.put("1", "yi");

          hm.put("2", "er");

          hm.put("3", "san");

          hm.put("4", "si");

          hm.put("5", "wu");

          Iterator it=hm.values().iterator();

          while(it.hasNext()){

          System.out.println(it.next());

          }

          } catch (Exception e) {

          e.printStackTrace();

          }

           

          Example 3: 同時獲得key和value

          try {

          HashMap hm = new HashMap();

          hm.put("1", "yi");

          hm.put("2", "er");

          hm.put("3", "san");

          hm.put("4", "si");

          hm.put("5", "wu");

          Iterator it=hm.entrySet().iterator();

          while(it.hasNext()){

          Map.Entry map=(Map.Entry)it.next();

          System.out.println(map.getKey()+" - "+map.getValue());

          }

          } catch (Exception e) {

          e.printStackTrace();

          }

          posted on 2007-09-06 12:58 龔椿深 閱讀(568) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 宜阳县| 克拉玛依市| 和静县| 乳源| 巨野县| 平定县| 十堰市| 恩平市| 普洱| 兴隆县| 本溪| 民和| 鸡泽县| 定襄县| 新和县| 娱乐| 潮州市| 泰来县| 改则县| 南召县| 保德县| 屏东市| 确山县| 同心县| 赤壁市| 呼和浩特市| 天祝| 绍兴市| 阆中市| 邯郸市| 福清市| 婺源县| 南江县| 八宿县| 塔城市| 独山县| 昭通市| 广宁县| 连城县| 富裕县| 南投县|