11

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.

1 Answer 1

16
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}
};
3
  • 1
    You beat me to it.
    – sfdcfox
    Aug 21, 2013 at 0:49
  • Good luck next time ;) Aug 21, 2013 at 0:51
  • 3
    The reason why: you can't construct an array of collections using the [] syntax. Why not? Who knows, but that's the way it's done. Also, lists can't have a terminating comma.
    – sfdcfox
    Aug 21, 2013 at 0:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .