Skip to main content
Commonmark migration
Source Link

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

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.

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.

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.

Source Link
Adrian Larson
  • 151.4k
  • 38
  • 247
  • 431

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.