How to create a List of Map objects

Is it possible to create a list of map objects? Here is what I am currently trying:

public List<Map<String, Integer>> slaMap = 
  new Map<String, Integer>[] {
    new Map<String, Integer> {'P1'=>30, 'P2'=>30},
    new Map<String, Integer> {'P1'=>60, 'P2'=>60},
    new Map<String, Integer> {'P1'=>120, 'P2'=>120},
  };

I can accomplish it using this method:

public List<Map<String, Integer>> slaMap = new List<Map<String, Integer>>();
slaMap.add(new Map<String, Integer> {'P1'=>30, 'P2'=>30});
slaMap.add(new Map<String, Integer> {'P1'=>60, 'P2'=>60});
slaMap.add(new Map<String, Integer> {'P1'=>120, 'P2'=>120});

However, I am wondering if it is possible using something similar to the first syntax I listed.

Answer

List <Map<String, Integer>> a = new List <Map<String, Integer>> {
 new Map<String, Integer>{'p1'=>10, 'p2'=>10}, 
 new Map<String, Integer>{'p1'=>20, 'p2'=>20}
};

Attribution
Source : Link , Question Author : Jason Sims , Answer Author : Jason Sims

Leave a Comment