How does one instantiate an array of maps in Java?

后端 未结 8 1265
刺人心
刺人心 2020-11-30 06:34

I can declare an array of maps using generics to specify the map type:

private Map[] myMaps;

However, I can\'t figur

8条回答
  •  余生分开走
    2020-11-30 07:10

    I know its a bit late to reply but I found this workaround helpful for my case...Hope it helps!

    Use an array of HashMap to store HashMaps..

    public static void main(String[] args) {
        HashMap[] arr = new HashMap[1];//creating an array of size one..just for sake of example
        HashMap arrMap = new HashMap();
    
        //use loops to store desired key-value pairs into the HashMap which will be stored inside the array
        arrMap.put("ABC", "Name");
    
        //use loop to store your desired hashMap into the array at a particular index
        arr[0] = arrMap;
    
        //desired manipulation of the stored array.
        System.out.println(arr[0]);
    }
    

提交回复
热议问题