少年阿賓

          那些青春的歲月

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks

          package com.abin.lee.sort;

          import java.util.Collections;
          import java.util.HashMap;
          import java.util.Iterator;
          import java.util.Map;

          public class CollectionIterator {
           /**
            * 創(chuàng)建二維MAP
            *
            * @return
            */
           public static Map<String, Map<String, String>> createMap() {
            Map<String, Map<String, String>> map2 = Collections
              .synchronizedMap(new HashMap<String, Map<String, String>>());
            Map<String, String> map1 = Collections
              .synchronizedMap(new HashMap<String, String>());
            Map<String, String> map3 = Collections
              .synchronizedMap(new HashMap<String, String>());
            map1.put("abin", "varyall");
            map1.put("abing", "boy");
            map1.put("peng", "boy");
            map1.put("pengzi", "man");
            map2.put("user", map1);

            map3.put("chinese", "beijing");
            map3.put("china", "shanghai");
            map2.put("contury", map3);

            return map2;
           }
           /**
            * 解析二維MAP
            * @param map
            * @return
            */
           
           public static String getMap(Map<String, Map<String, String>> map) {
            for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext();) {
             Map.Entry entry=(Map.Entry)iterator.next();
             //先遍歷一維map
             System.out.println("one map name="+entry.getKey());
             System.out.println("one map name="+entry.getValue());
             Map<String, String> map1=(Map<String, String>)entry.getValue();
             if(entry.getValue() instanceof Map){
              for(Iterator it=map1.entrySet().iterator();it.hasNext();){
               Map.Entry entry2=(Map.Entry)it.next();
               //再遍歷二維map
               System.out.println("two map name="+entry2.getKey());
               System.out.println("two map name="+entry2.getValue());
              }
             }
            }

            return null;
           }
           public static void main(String[] args) {
            Map<String, Map<String, String>> map=createMap();
            getMap(map);
            
           }
          }

          posted on 2012-08-18 15:51 abin 閱讀(4192) 評論(1)  編輯  收藏 所屬分類: java集合類

          Feedback

          # re: Java 遍歷嵌套Map 2014-12-11 21:05 阿斯
          1212  回復(fù)  更多評論
            

          主站蜘蛛池模板: 襄城县| 永平县| 故城县| 丰都县| 镇坪县| 延安市| 横峰县| 互助| 江山市| 万山特区| 津市市| 大理市| 育儿| 诏安县| 札达县| 平泉县| 平顺县| 会同县| 潞西市| 平罗县| 横山县| 江津市| 襄城县| 瑞金市| 准格尔旗| 阳泉市| 宕昌县| 洱源县| 咸丰县| 德清县| 都安| 扶绥县| 隆尧县| 神农架林区| 鄂伦春自治旗| 板桥市| 开封市| 班玛县| 鸡西市| 綦江县| 陆良县|