function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
steve nabors 7steve nabors 7 

Which is better in an iteration over a Map, get methods or an object instance?

Which is better in an iteration over a Map,  get methods, or an object instance?

Example:

Map<Id, SObject> mapName = new Map<Id, SObject>([ SOQL Query ]);
for (Id key : mapName.keySet()){

EITHER THIS:
var1 = mapName.get(key).field1
var2 = mapName.get(key).field2
var3 = mapName.get(key).field3
.......

OR THIS:
SObject o = mapName.get(key);
var1 = o.field1
var2 = o.field2
var3 = o.field3
.......

 
Agustin BAgustin B
Hi Steve from my point of view the best would be to use the get because using an SObject means more memory being used as you are creating a new variable.
Also you can see on the documentation that salesforce uses the get method to retrieve the data but does not compare this exact situation.

Here you have other considerations for map:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections_maps.htm

If it helps please mark as correct, it may help others.
AbhishekAbhishek (Salesforce Developers) 
Hi Steve,

You can try this ,

https://developer.salesforce.com/forums/?id=906F00000008xHJIAY

It might help you.

Thanks.