Java: create a list of HashMaps

前端 未结 6 579
甜味超标
甜味超标 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条回答
  •  萌比男神i
    2021-01-02 02:47

    Something which is maybe also worth mention it, is that you should define the type of the elements you use in the List, for the HashMap its not possible because you are mixing Integers and Strings.

    And another thing is that you should use the List interface as type, so you are able to change the implementation (ArrayList or whatever) in the future.

    Here the corrected code:

    Map mMap = new HashMap();
    List list = new ArrayList();
    

提交回复
热议问题