Java HashMap associative multi dimensional array can not create or add elements

前端 未结 4 1238
小蘑菇
小蘑菇 2020-12-09 17:33

Okay so I have spent several hours trying to wrap my head around this concept of a HashMap in Java but am just not able to figure it out. I have looked at many tutorials but

4条回答
  •  失恋的感觉
    2020-12-09 18:02

    HashMap> myArray = new HashMap>();
    
    if (!myArray.containsKey("en")) {
        myArray.put("en", new HashMap());
    }
    myArray.get("en").put("name", "english name");
    

    In Java you have to be explicit about when you are creating an object. In this case first we check if there is already a HashMap object stored in our outer HashMap under the key "en". If not, we create an empty one.

    Now to put a new value into it we have to first get it from the outer HashMap, then put the new value.

提交回复
热议问题