0

I'm trying to fill a Map < Id, List< sObject>> by looping other the Map.keySet(), filling the list of sObjects, affecting it, and then go to the next key.

I read somewhere that it was a good practise to instanciate my temporaryList once before looping, begin to loop : filling it, affecting it the the Map.get(key), and then clearing it after use (to be able continue treatment).

Something like this :

// instanciating once a tempList
List<sObject> tempList = new List<sObject>();
for(Id id : Map.keySet()) {
    for(sObject obj : anotherList) {
        tempList.add(obj);
    }

    // then add to the Map as value for the actual 
    // if not deepCloning, it's erased two lines after ... (or I may miss something )
    Map.put(id, tempList.deepClone());

    tempList.clear();

}

I did it and first got a problem because of the passing by reference, so I used list.deepClone() and it went well.. I mean after filling my tempList and affecting it to the Map.get(key), clearing the tempList also clear the Map.get(key) and that, of course, wasn't what I was trying to do.

Naturally, that would not be my first way of doing it, I'll do it like this :

For(Id id : Map.keySet()) {
  // declaring here the List to fill
  List<sObject> tempList = new List<sObject> ();

  for(sObject obj : anotherList) {
      tempList.add(obj);
  }
   // then add to the Map as value for the actual key
   Map.put(id, tempList);
}

But it's on the first hand only, within a loop, a declaration of a new List<> On the other hand, instanciating it before loops, using it, deepcloning the list to feed the Map.values(), and then clearing it to be able to use it for the next key in map.keySet() we have to fill...

Is really the second method the best ? I got some issues understanding every aspect of POO references passing

5
  • the second is best and typical
    – cropredy
    Aug 8, 2019 at 5:35
  • Instanciating it once, then deepcloning and clearing ? Ok, then I'll have to work on it Thanks ! EDIT : well, the first code and the second are in reverse order of my explanation (my bad), but if you say 'typical', I'd say you're speaking about instanciating the new List within the loop ? (I should work on passing by reference) Aug 8, 2019 at 7:51
  • The cloning should not be required
    – cropredy
    Aug 8, 2019 at 13:09
  • If not, when clearing the temporaryList, I got my Map.values deleted ... :( I've made so much tryied about this yesterday / this morning, that I think I've got to way a little in order to have it clearly in my head. I'll have to test another use cases. Thanks anyway :) Aug 8, 2019 at 13:16
  • 1
    Hmm. I may have misunderstood your issue. I'll look at it later today.
    – cropredy
    Aug 8, 2019 at 13:18

1 Answer 1

1

I may still not be understanding your issue but I tried to reproduce it with the simplest code

Map<Id, SObject[]> sobjectListsById = new Map<Id,SObject[]> {  // "MAP" orderIds =>accounts
        fflib_IDGenerator.generate(Order.SObjectType) => new List<SObject>(),
        fflib_IDGenerator.generate(Order.SObjectType) => new List<SObject>(),
        fflib_IDGenerator.generate(Order.SObjectType) => new List<SObject>()
};
Account[] accounts = [SELECT Name From Account Limit 2];  // "anotherList"

for (Id sobjId : sobjectListsById.keySet()) {
    sobjectListsById.put(sObjId,accounts);
}
String[] results = new List<String>();
for (Id id : sobjectListsById.keySet()) {
    results.add(id + ':' + String.valueOf(sobjectListsById.get(id)));
}
system.debug(logginglevel.INFO,'\n'+String.join(results,'\n'));

and the results are as expected - each key in "MAP" contains the "AnotherList":

801000000000001AAA:(Account:{Name=France Test Account, Id=0010q00000CtA6lAAF}, Account:{Name=TEST ACCOUNT JAPAN, Id=0010q00000CsINjAAN})
801000000000002AAA:(Account:{Name=France Test Account, Id=0010q00000CtA6lAAF}, Account:{Name=TEST ACCOUNT JAPAN, Id=0010q00000CsINjAAN})
801000000000003AAA:(Account:{Name=France Test Account, Id=0010q00000CtA6lAAF}, Account:{Name=TEST ACCOUNT JAPAN, Id=0010q00000CsINjAAN})

You must log in to answer this question.

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