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 龔椿深 閱讀(567) 評論(0)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 毕节市| 木兰县| 桐庐县| 仁寿县| 佛冈县| 河北区| 宁乡县| 凤山市| 新泰市| 自贡市| 巴青县| 察哈| 宿松县| 大田县| 邵东县| 庐江县| 吴江市| 中方县| 新宁县| 黎川县| 进贤县| 侯马市| 浑源县| 同德县| 三亚市| 新巴尔虎右旗| 鹤壁市| 新宾| 遂平县| 永定县| 贡觉县| 苏州市| 永城市| 广宗县| 宁德市| 神农架林区| 普洱| 天镇县| 柳河县| 郸城县| 布拖县|