2

I am trying to reduce the searching area inside a loop once i have satisfied some condition. That means i want to reduce the size of the map by removing the keys in it. Those keys and the related data have already been processed so i am thinking it is unnecessary to having them in the map and try to avoid going through them once again inside the loop. Is that possible? Please suggest.

Here, mpSFDCSAP is a map.

            for(SAP_Customer_Master__c s: mpSFDCSAP.values()) {
                String[] strArr2 = s.Customer_Name__c.split(' ');
                String strCustName = '';
                if((strArr2[0].length()==1 && strArr2[0].StartsWith('A')) || (strArr2[0].length()==3 && strArr2[0].StartsWith('The'))) {
                    strCustName = strArr2[1].toLowerCase();
                }else{
                    strCustName = strArr2[0].toLowerCase();
                }

                if(strCustName.startsWith(strAccName)) {
                    lstSAPCustMast.add(s);
                }       
             mpSFDCSAP.remove(some keys); 
            //I am thinking the above line would reduce my map size 
            //and improve the performance of the loop by 
           //preventing to lookup the visited records once again.
           //Does it work inside the context of a loop, because loop variable is the map itself
            } 
1
  • What are you actually saving whilst in the loop? If you want to clear down the map after you've iterated it, you can use mpSFDCSAP.clear(). You've already taken the hit on heap size when you load the map so I don't see the point of clearing it whilst your inside the loop Jul 29, 2014 at 20:31

2 Answers 2

9

You must not modify a collection of any sort while it is an iterator loop. This may cause erratic or fatal behavior. That includes adding elements to the collection, removing them, or re-ordering them via sort.

What you can do, however, is clone the map's keys, and then iterate over those:

for(String key:myMap.keySet().clone()) {
    // myMap.get(key), process that value
    myMap.remove(key);
}

You can use this technique when you need the heap space freed as soon as possible.

-1

Reducing key size for performance reasons won't help, as when working with a map, one doesn't ever iterate over the keyset - you just do a Map.get(key). In your case, you're iterating over all the values, which means I have to ask:

Why are you using a map and iterating over values? If you only need the values, then just make a list and don't even have a map.

It's an interesting idea, but I don't think it will work, and it's unnecessary. I don't see why you would need to prevent looking in visited records, as you're just iterating through the list once.

1
  • 1
    The point is this. Let's say the map uses 60% of the memory, and the resulting output from the map processing will use 80% of heap. This means that not freeing memory would hit 140% heap. By reducing as you go, you get more free space for output. I have had to use this technique on some aggressively large data sets. It's true that most developers won't ever have a need for this trick, but it's far better to have and not need than to need and not have.
    – sfdcfox
    Jul 30, 2014 at 0:32

You must log in to answer this question.

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