I currently have a multi-demensional map/list object that is defined like this:

Map <Id,List <Map <Id,Custom_Inventory__c> >> complexMap = getComplexMapObject();

The object was difficult to create but it works perfectly fine, the first Id is AccountId, and the second index is ProductId.

Now I need a method that uses this object to verify if a record has a matching Account and Product Id within this collection. The first check is easy, something like this:

    return FALSE;

The question I have is about the deeper index. I know I can loop through the map list within but wondering if I have to loop if I know what index I'm looking for. For example:

for (Map <Id,Custom_Inventory__c> inventoryMap : complexMap.get(variable.Account__c) ) {
    // Iterate through inventoryMap, but I already know which 
    // index within this map I need... variable.Product__c)
        return TRUE;
    } else {
        return FALSE;

If I can avoid the above loop I would like to. It will never be a very large loop but if I can avoid it I think its for the better.

  • 1
    Why is the inner collection List<Map> instead of just Map? If it weren't for that I would say your life would be a lot easier with complex keys instead of complex data structure.
    – Adrian Larson
    Jul 2, 2015 at 22:01
  • I needed the list because I have multiple products under the same Account, so I can't have multiple index entries of Account - Product maps. So this makes it possible to have an index of all related products by account. Open to other ideas but this is currently working. Jul 2, 2015 at 22:05
  • Isn't the Map key the Product id though? So Map<Id, Map<Id, Custom_Inventory__c>> seems sufficient. Or if you have multiple matches, use Map<Id, Map<Id, List<Custom_Inventory__c>>>. Or better yet Map<DualIdKey, List<Custom_Inventory__c>>.
    – Adrian Larson
    Jul 2, 2015 at 22:12
  • Last time I had this issue the DualIdKey idea was the perfect solution. In this instance I have 2 separate checks I need though, if the account is not part of the program then I need to flag it, and then if it is then I need to verify the products for that account are within the program. Originally I tried <Map Id, Map<Id, Custom_Inventory__c>> but had trouble verifying if a product exist within a specific account. Not sure how I would do that. Jul 2, 2015 at 22:24

1 Answer 1


If it's just a contains check, then an additional, set holding all the ProductId values would be the simplest approach to avoiding the iteration over the AccountId values and would probably make the code a bit clearer too. The set could be generated from the complexMap using:

Set<Id> productIds = new Set<Id>();
for (List<Map<Id, Custom_Inventory__c>> l : complexMap.values()) {
    for (Map<Id, Custom_Inventory__c> m : l) {

or returned from the getComplexMapObject method using a simple class with a field for the set and a field for the map and any methods that help.

But if your present code works and there are only ever a few inventory maps I'd stick with the current code.

(The equivalent set for the AccountId values is complexMap.keySet().)

  • Might be worth mentioning how to get the Account ids, even if it is much more straightforward.
    – Adrian Larson
    Jul 2, 2015 at 22:06
  • @AdrianLarson Done.
    – Keith C
    Jul 2, 2015 at 22:09

You must log in to answer this question.

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