Java: create a list of HashMaps

前端 未结 6 594
甜味超标
甜味超标 2021-01-02 02:36

I tried to create a list of maps. In the following code, I\'m expecting to get

[{start=1,text=ye}, {start=2,text=no}]

however, I only got

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-02 03:08

    When you put the same key name in the map then values will be overridden to same key. suppose we have

    mMap.put("start",1);
    mMap.put("start",2);
    mMap.put("start",3);
    mMap.put("start",4);
    

    it will not make map of length 4, as the key("start") is same so it will override the values on the same key. so you will get the get only one value (4) against "start" key. To avoid this you will have to change the name of keys in a hashmap but in your scenaro, you need an other instance of hashmap to save the key and values as you are maintaining the arralist.

提交回复
热议问题