How do I access nested HashMaps in Java?

后端 未结 10 767
再見小時候
再見小時候 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:48

    Yes, if you use the proper generic type signature for the outer hashmap.

    HashMap> hm = new HashMap>();
    // populate the map
    hm.get("keyname").get("nestedkeyname");
    

    If you're not using generics, you'd have to do a cast to convert the object retrieved from the outer hash map to a HashMap (or at least a Map) before you could call its get() method. But you should be using generics ;-)

提交回复
热议问题