写一个递归方法,传入一个Map,返回这个Map对象中一共有多个Map类型的对象 Map中是可以添加Map,被添加的这个Map也可以添加Map,所以需要判断传入的Map对象的所有子节点,如果是Map就

来源:互联网 发布:ubuntu codeblocks 编辑:程序博客网 时间:2024/04/30 06:29
@Test
public void test1() {
Map<String, String> m0 = new HashMap<String, String>();
m0.put("a", "a");
Map<String, Map> m1 = new HashMap<String, Map>();
m1.put("a", m0);
Map m2 = new HashMap();
m2.put(m0, m1);
Map m3 = new HashMap();
m3.put(m2, m2);

System.err.println(getdep(m3));

}
static int i = 0;
public static int getdep (Map map) {
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Entry) it.next();
Object val = entry.getValue();
Object key = entry.getKey();
if (val instanceof  Map) {
i++;
getdep((Map)val);

if (key instanceof Map) {
i++;
getdep((Map)key);
}
}
return i;
}
1 0