How do I access nested HashMaps in Java?

后端 未结 10 771
再見小時候
再見小時候 2020-12-01 09:28

I have a HashMap in Java, the contents of which (as you all probably know) can be accessed by

HashMap.get(\"keyname\");

If a have a HashMap

10条回答
  •  春和景丽
    2020-12-01 09:36

    You can do it like you assumed. But your HashMap has to be templated:

    Map> map = 
        new HashMap>();
    

    Otherwise you have to do a cast to Map after you retrieve the second map from the first.

    Map map = new HashMap();
    ((Map)map.get( "keyname" )).get( "nestedkeyname" );
    

提交回复
热议问题