Monday, April 27, 2015

Salesforce Summer 15 New Feature || Predictable Iteration Order for Apex Unordered Collections

After Summer15 Salesforce release, the order of elements in unordered elements (Map and Set) is always the same. Previously the order was arbitrary and as a developer, you have no control on the order.

Now as you can see, with this new feature, we are trying to order the unordered elements. A little strange to me. But if you have some code snippet, where the logic depends on the ordering of the elements, then this new feature will be really helpful.

Let's consider the below code snippet -

If you run this code after Summer15, you will get the elements in the same order every time you execute.

21:01:19.145 (145215707)|USER_DEBUG|[10]|DEBUG|Index: 1 Employee: James
21:01:19.145 (145338304)|USER_DEBUG|[10]|DEBUG|Index: 2 Employee: Sudipta
21:01:19.145 (145498650)|USER_DEBUG|[10]|DEBUG|Index: 3 Employee: Christiaan
21:01:19.145 (145587990)|USER_DEBUG|[10]|DEBUG|Index: 4 Employee: Victor

So now your Unordered Collections are also predictable.

0 comments:

Post a Comment