当前位置:   article > 正文

java 遍历嵌套map_Java 遍历嵌套Map

java遍历嵌套map

package com.abin.lee.sort;

import java.util.Collections;

import java.util.HashMap;

import java.util.Iterator;

import java.util.Map;

public class CollectionIterator {

/**

* 创建二维MAP

*

* @return

*/

public static Map> createMap() {

Map> map2 = Collections

.synchronizedMap(new HashMap>());

Map map1 = Collections

.synchronizedMap(new HashMap());

Map map3 = Collections

.synchronizedMap(new HashMap());

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> 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 map1=(Map)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> map=createMap();

getMap(map);

}

}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/489064
推荐阅读
相关标签
  

闽ICP备14008679号