4

In this list to list code where you find Id's on a query and insert the Id's into another pre-existing list worse, better, or same as the map to map code below it? I am trying to use maps to be easier on limits, however, I don't see an advantage when having to still iterate over both maps. They both take the same amount of iterations to finish, in about the exact same lines of code. List to List version:

List<Account> uniqueIdMatchAcc = new List<Account>();
if(allUniqueIDs.size()>0){
  uniqueIdMatchAcc = [Select id, unique__c From Account Where unique__c in :allUniqueIDs];
}  
if(uniqueIdMatchAcc.size() > 0 ){ //if unique matches were found, feed Id's into registrationList created earlier
   for(Account uma : uniqueIdMatchAcc){
        for(registration crl : registrationList){
            if(uma.unique__c == crl.acc.unique__c){
                crl.acc.Id = uma.Id;
            }
        }
    }
}

Map to Map version, better to use, worse, or same?

        Map<String, account> uniqueIdMatchAccMap = new Map<String, account>();         
        for(Account a: [Select unique__c From Account Where unique__c in :allUnique]){
            uniqueIdMatchAccMap.put(a.unique__c, a);
        }
        if(uniqueIdMatchAccMap.size() > 0 ){
            for(String unique : uniqueIdMatchAccMap.keySet()){
                for(Integer i : accountMap.keySet()){
                    if(accountMap.get(i).unique__c == unique){
                        accountMap.id.put(i, uniqueIdMatchAccMap.get(unique));
                    }
                }
            }
        }

Could I be doing this better?

Adding to this question, part 2, how would I get it to add the account into accountMap without erasing everthing that was originally in the map. accountMap.put(i, ufidMatchAccMap.get(unique)) wipes out all that was and insterts only the Id, where as I am looking to put in only the Id and leave what was as well?

1 Answer 1

5

Your second solution is even worse in aspect of efficiency. Because Map in apex is implemented in HashMap and get is of logN complexity if I remember correctly. But this does not mean you shouldn't be using Map in this case. What you need to do is to change the second version into this:

if(uniqueIdMatchAccMap.size() > 0 ){
    for(registration rl : registrationList){
        Account a = uniqueIdMatchAccMap.get(rl.acc.unique__c);
        if(a != null)
        {
            rl.acc.Id = a.Id;
        }
    }
}

As you have already put everything into a map, there is no reason you should loop through the map's keySet in this case. And since you get rid of one level's loop and use map .get() method instead, the efficiency of this code will be significantly better.

7
  • Sorry @Lance Shi, I just corrected the map to map version of the code, I posted the map to list version on accident. I have been toying with this all day and grabbed the wrong chunk of code. I do understand what you are saying though, thank you. I also posted a follow up question as well at the bottom of my original question, any idea on that one? Feb 7, 2016 at 22:05
  • P.S. I just have to say, what you did post is beautiful :) Feb 7, 2016 at 22:07
  • 2
    Minor optimization (which I was in the process of writing in my own answer), would read something like: if(uniqueIdMatchAccMap.containsKey(rl.acc.unique__c)) { rl.acc.Id = uniqueIdMatchAccMap.get(rl.acc.unique__c).Id; } And, yes, Maps seem to run in about O(log n) time, which is still far better than the 0(n^2) time that a double loop requires.
    – sfdcfox
    Feb 7, 2016 at 22:17
  • 1
    @sfdcfox Thank you for that. I was thinking about that. But is there any difference between map.containsKey(A) and map.get(A) != null?
    – Lance Shi
    Feb 7, 2016 at 22:44
  • 2
    The optimization is mostly about skipping the intermediate variable, which probably adds more overhead because of the heap allocation/deallocation, although checking for a key twice might be more costly in large maps. That said, we could avoid the second key hit using a little known assignment feature. First, we declare an Account variable, then we can write: if((tempAccount = uniqueIdMatchAccMap.get(rl.acc.Unique__c)) != null) { rl.acc.Id = tempAccount.Id; } However, that is pretty much in the land of microoptimization...
    – sfdcfox
    Feb 7, 2016 at 23:01

You must log in to answer this question.

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