Print this page

How to test for the Critical Update: Predictable Iteration Order for Unordered Collections

Knowledge Article Number 000213749
Description

On March 24, 2015 we have introduced a critical update that makes the order of elements in unordered collections (Map & Set) deterministic. Customers now have the opportunity to test against this update prior to an auto-activation with the Summer ‘15 release.

How should this be tested ?
Maps and Sets anyways do not have a get(Integer) method to specify order, so how can today's code itself have an order specified. We can imagine Set data being added to List, but then one is working off the list and not the set, which should be acceptable.

Resolution

The order has always been deterministic, although by coincidence rather than by design. That is the main reason for the critical update. Customers may be relying on this deterministic ordering without even being aware of that reliance.

We saw tests where objects in a map.values() set were being serialized to JSON, and where sets were being compared using toString(). These things will all be the same from release to release, despite the fact that the order is technically not deterministic.

Because people are relying on the deterministic order, we are establishing a deterministic order from this point forward with this update.

We are making necessary changes to the lower-level infrastructure that would change the order from current-deterministic-order to some-other-deterministic-order. If we did these changes without this critical update, there would be test failures in a sizable number of Orgs (blocking deployments for those Orgs) and there would be application failures to match these test failures.

You can test this feature using below snippet of code for (Map and Set) collections. 

Set<String> set1 = new Set<String>();

System.debug('Put random values...');

set1.add('S1X');

set1.add('S5X');

set1.add('S4X');

set1.add('S3X');

set1.add('S6X');

set1.add('S2X');

set1.add('S8X');

set1.add('S7X');

set1.add('S9X');

System.debug('System.debug() shows the values ordered: ' + set1);

System.debug('The set iterates over the original order...');

for (String s : set1) {

System.debug(s);

}

System.debug('JSON.serialize() shows the original order...');

System.debug(JSON.serialize(set1));

(Or)

Map<String,String> sM = new Map<String,String>{

'3' => 'A',

'5' => 'C',

'4' => 'B'

};

sM.put('6','X');

sM.put('7','Y');

sM.put('1','P');

sM.put('0','Z');

for(String s : sM.keySet()) {

System.debug(s + ' > ' + sM.get(s));

}

With CRUC Activated: The iteration order of unordered collections (Map and Set) is deterministic. That is, the order of elements in maps and sets is the same each time your code is run

With CRUC Deactivated: It iterates in random order.





promote demote