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<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);
}
}