4

A few years back, there was a release that made Maps and Set ordered like list, But it looks like it's not the case anymore.

Set<String> newSet = new Set<String>();
newSet.add('Experian');
newSet.add('Equifax');

System.debug(newSet); //Prints {Equifax, Experian} instead of right order.

The reason I wanted to use set was to not let duplicate values, but let the order maintained. Am I doing something stupid?

Edit: It looks like when you iterate it in For loop FIFO order is maintained.

Set<String> newSet = new Set<String>();
newSet.add('Experian');
newSet.add('Equifax');
newSet.add('Transunion');


for(String str : newSet){
    System.debug(str);
}
System.debug(newSet);

OP :

Experian
Equifax
Transunion

{Equifax, Experian, Transunion}

Edit 2: Checking if 2 sets having identical values iterate in same order

Set<String> newSet1 = new Set<String>();
newSet1.add('Experian');
newSet1.add('Equifax');
newSet1.add('Transunion');


for(String str : newSet1){
    System.debug(str);
}
System.debug(newSet1);



Set<String> newSet2 = new Set<String>();
newSet2.add('Transunion');
newSet2.add('Experian');
newSet2.add('Equifax');



for(String str : newSet2){
    System.debug(str);
}
System.debug(newSet2);

OP :

Experian
Equifax
Transunion
{Equifax, Experian, Transunion}
-----------------------------------
Transunion
Experian
Equifax
{Equifax, Experian, Transunion}
4

1 Answer 1

8

The change that was released a few years ago does not guarantee that elements of an unordered collection will iterate in the same order they were added. Rather, it guarantees that a collection with the same elements will always iterate in the same order.

Iteration Order for Maps and Sets Is Now Predictable

The order of elements in unordered collections (Map and Set) is now the same each time your code is run. Previously, the order of elements in unordered collections was arbitrary, and you couldn’t rely on the order of elements in maps and sets.

You can see that the iteration order is the same when the elements are the same with this simple script:

Set<String> a = new Set<String> { 'Experian', 'Equifax' };
Set<String> b = new Set<String> { 'Equifax', 'Experian' };
system.assertEquals(a, b, 'The collections should be equal');

// since the collections are identical,
// they should iterate in the same order
system.debug(a);
system.debug(b);

Please note also:

Ideally, you don’t want your code to depend on the order of elements in unordered collections.

3
  • Strange, if you iterate it in for loop, the order is always FIFO, Commented Jul 24, 2018 at 19:24
  • I think FIFO would be a bug. According to everything I read, if you generate a collection with the same elements, it should iterate in the same order regardless of how you put them in.
    – Adrian Larson
    Commented Jul 24, 2018 at 19:33
  • The order you get in System.debug(set) and in for loop is different, I suspect Set stores it in FIFO, but when toString method is called, it uses hashing algo to sort it, Commented Jul 24, 2018 at 19:50

You must log in to answer this question.

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