# What is the best approach (shorthand form) to iterate a collection of List with varying size?

I have a collection of list which is,

``````List<List<String>>
``````

Now, i am iterating it as follows,

``````for(List<List<String>> n: mapSAP.values()) {
for(List<String> l: n) {
System.debug('internal list:'+l);
System.debug('internal list size:'+l.size());
SalesOrder sobj = new SalesOrder();
sobj.OrderNo = l;
sobj.SalesOrg = l;
sobj.DocumentDate = Date.valueOf(l); //Utility.FormatDate(l);
sobj.PurchaseOrderNo = l;
sobj.Amount = l;
amt += Double.valueOf(l);
sobj.Curren = l;
if(l.size() == 11) {
sobj.Product = l;
} else {
sobj.Product = '';
}
cnt++;
}
``````

The internal list "l" comes in varying size sometimes it comes with size 8 sometimes it comes as 9 or 10 or 11. Now i am checking the list size using if condition (like if(l.size() == 11 then access 11th element) before accessing the data using the index value like l or l. Is there any other best approach to deal or access the elements of list with varying size without much conditional checks as i did above?

Otherwise, without if conditions i am getting List out of bound exceptions when the list comes with size 10 and trying to access the data at 11th position. For the above usecase, the maximum list size is 11.

Update:

Salesorder is the custom wrapper class and not an Sobject.

Sometimes, I use patterns like this where I don't want to go through the trouble of defining an SObject to provide the mapping rules (which I would do if the rules were to change often or be expanded over time)

``````Map<String,Map<Integer,Integer> fldnameToListSizeToListPosMap =
new Map<String,Map<Integer,Integer> {
'orderno' => new Map<Integer,Integer> {8 => 3, 9=> 3, 10=> 3, 11=>3},
'product' => new Map<Integer,Integer> {8 => 8, 9=> 9, 10=> 10, 11=>10},
...
};

sObj.orderNo = fldnameToListSizeToListPosMap.get('orderno').get(l.size());
sObj.product = fldnameToListSizeToListPosMap.get('product').get(l.size());
...
``````

Makes code look cleaner than lots of if statements. The above presumes some regularity in expected list sizes but you should get the idea.

You could use ternary operator, e.g.:

``````for (...) {
for (...) {
sobj.A = (l.size() >= 1)  ? l  : '';
sobj.B = (l.size() >= 2)  ? l  : '';
...
sobj.J = (l.size() >= 10) ? l  : '';
sobj.K = (l.size() >= 11) ? l : '';
}
}
``````

Don't think there really is a best approach in this case. It's just the matter of taste and/or habit.
{"ft_lang_label":"__label__en","ft_lang_prob":0.64791334,"math_prob":0.94484174,"size":1288,"snap":"2019-51-2020-05","text_gpt3_token_len":344,"char_repetition_ratio":0.115264796,"word_repetition_ratio":0.0,"special_character_ratio":0.29347825,"punctuation_ratio":0.176259,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9711011,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-01-21T18:12:28Z\",\"WARC-Record-ID\":\"<urn:uuid:e5d62703-0483-4d75-851c-b39419a58d19>\",\"Content-Length\":\"140159\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:5249e965-582b-47d7-8545-b1b1c4938807>\",\"WARC-Concurrent-To\":\"<urn:uuid:4664a465-32e8-49fe-a123-bc4750d3a3e7>\",\"WARC-IP-Address\":\"151.101.129.69\",\"WARC-Target-URI\":\"https://salesforce.stackexchange.com/questions/33890/what-is-the-best-approach-shorthand-form-to-iterate-a-collection-of-list-with\",\"WARC-Payload-Digest\":\"sha1:3NIG5LSBOHO3CJ4SV7ZD6HQOSERZE6BS\",\"WARC-Block-Digest\":\"sha1:BQLHHWFPMXLDVX3TBAW5UP24QJKF3CLK\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-05/CC-MAIN-2020-05_segments_1579250604849.31_warc_CC-MAIN-20200121162615-20200121191615-00352.warc.gz\"}"}