6

I am trying to add values to a list iterating through a for loop like this.

List<String> leftvalues = new List<String>();
for(OpportunityLineItem  o: OLI_List)
{
    //Gets product name from map and adds to list
    leftvalues.add(pbeId_PrNameMap.get(o.PricebookEntryId));
    System.debug('*****Leftvalues:'+leftvalues);
} 

Strangely its sorting internally after adding instead of adding at the end of list each time. What am i doing wrong .

Debug log results:

05:29:24.463 (463698000)|USER_DEBUG|[77]|DEBUG|***Leftvalues:{17TBUPGRADE}

05:29:24.464 (464053000)|USER_DEBUG|[77]|DEBUG|***Leftvalues:{17TBUPGRADE, 5UPB-C10/W}

05:29:24.464 (464397000)|USER_DEBUG|[77]|DEBUG|***Leftvalues:{17TBUPGRADE, 5UPB-C10/W, IFU-PN100M}

05:29:24.464 (464739000)|USER_DEBUG|[77]|DEBUG|***Leftvalues:{17TBUPGRADE, 5UPB-C10/W, CBK-UPG01, IFU-PN100M}

Why did new value CBK-UPG01 go to third position instead of fourth.?

How to prevent it from re positioning itself within the list ? Am i doing something wrong ?

4
  • can you share the whole method? Commented Dec 30, 2013 at 11:24
  • 4
    Your code looks ok to me, but what I'm starting to wonder is if you can rely on the conversion from list<string> -> string when you are printing it with System.debug. Could you verify after the for-loop that the values are infact out of order (by for-looping through the list)?
    – haagen
    Commented Dec 30, 2013 at 11:35
  • 1
    agree with @haagen Commented Dec 30, 2013 at 11:42
  • 1
    Lists are guaranteed to be ordered, also when casting to String. Will you extend debugging by logging o value as well? I bet the issue is with the order of OLI_List.
    – endrju
    Commented Dec 30, 2013 at 14:12

1 Answer 1

14

The code that you've provided doesn't match the debug log. Maybe someone concurrently changed the code while you were running it, or you're just testing us ;-) It doesn't really matter - the question is a great puzzle!

Let's have a quick look at the documentation. First of all, Lists are guaranteed to be ordered and the method add always puts an element at the end of a List. The described behavior would mean a serious bug in the Salesforce List implementation, that has been used by almost every APEX program for ages with no complains.

Even more important clue is given the kind of brackets in the debug log. The specification of String.valueOf suggests that Lists are serialized using parentheses ( ), and a quick test shows that curly brackets { } are reserved for Sets.

Conclusion: the debug log presents an algorithm using a Set. By the way it's worth noting that in Salesforce Sets are implemented using hashes and do not guarantee any order (but it might happen that Set values are sorted when casting to String).

2
  • Good catch - I learned something new here!
    – haagen
    Commented Dec 30, 2013 at 21:33
  • Such a good answer. Such a shame the OP didn't take the time to accept your answer. Commented Apr 16, 2015 at 20:20

You must log in to answer this question.

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