0

I am learning to code in apex. i just want to know what mapping is and in what cases i can maximize the usage of mapping in coding. links of articles is appreciated but explaining in simple words with sample code is better. Thanks in advance.

2

First, Map is a collection type. It is quite similar with Set and List. The difference, is that map is a collection of key-value pairs where each unique key maps to a single value object that could be a single value, another collection, an sObject, etc. Keys can be any primitive data type, while values can be a primitive, sObject, collection type or an Apex object. To know more about MAP you can view the documentation in this link.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_map.htm

A basic example of using map is this, Let say you want to store a student name and its age.

Map<String,Integer> yourVariable = new Map<String,Integer>();
yourVariable.put('John', 8);
yourVariable.put('mark', 9);
yourVariable.put('mark', 10);

The Student name is your key and the age is your value. Using the sample code above, you now have a collection of Student with their age. Hope this Help.

1

Map is Key value pair. Its methods can be used to optimize the code. You can find colletion methods in below link: http://www.salesforcetutorial.com/salesforce-collections/

You can use as per the case involved. for example you want list and List, Instead of using two list you can use map as Map where Id is key is accountId and Value will be Account Object. So for iterating over AccountId's you can use map.keyset() and to iterate over All accounts you can use Map.Values().

Let me know if you need more clarity here!!

  • so i can get all the other fields just by using a single map in an object? – sfdclearner Feb 19 '16 at 7:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.