map遍历问题

有一个map如下:
map.put(key,value);
key =components
value= {
component_1={backurl_1=xxxxxxxxxxx, backdate_1=2010-10-21, backname_1=back1},
component_2={backdate_2=2010-10-21, backurl_2=xxxxxxxxxxx, backname_2=back1}
}

解析value下面的component_1 component_2,然后遍历下面的backurl_1、backdate_1、backname_1,求解.

2个回答

[code="java"]
public static void main(String[] args)
{
Map map = new HashMap();

    Map<String, Object> nextMap = new HashMap<String, Object>();

    Map<String, Object> nextMap1 = new HashMap<String, Object>();

    map.put("components", nextMap);

    nextMap.put("component_1", nextMap1);

    nextMap1.put("backurl_1", "xxxxxxxxxxx");
    nextMap1.put("backdate_1", "010-10-21");
    nextMap1.put("backname_1", "back1");

    nextMap1 = new HashMap<String, Object>();
    nextMap.put("component_2", nextMap1);

    nextMap1.put("backdate_2", "2010-10-21");
    nextMap1.put("backurl_2", "xxxxxxxxxxx");
    nextMap1.put("backname_2", "back1");


    for (Entry<String, Object> entry : map.entrySet())
    {
        System.out.println("key = " + entry.getKey());
        for (Entry<String, Object> entryi : ((Map<String, Object>) entry
                .getValue()).entrySet())
        {
            System.out.println("key = " + entryi.getKey());
            for (Entry<String, Object> entryj : ((Map<String, Object>) entryi
                    .getValue()).entrySet())
            {
                System.out.println(entryj.getKey() + " = "
                        + entryj.getValue());
            }

        }
    }

    // map.put(key,value);
    // key =components
    // value= {
    // component_1={backurl_1=xxxxxxxxxxx, backdate_1=2010-10-21,
    // backname_1=back1},
    // component_2={backdate_2=2010-10-21, backurl_2=xxxxxxxxxxx,
    // backname_2=back1}
    // }
}

[/code]

不知道这是不是你想要的。

component_1把这个抽象成一个类不就得了。!
枚举一次就好了。

立即提问
相关内容推荐