+ Start a Discussion
Girish Reddy 52Girish Reddy 52 

What is the return type of map in apex ?

PriyaPriya (Salesforce Developers) 

Hi Girish,

Map is a datatype. It has collection of string and values. 

For detail, kindly refer this link :- 

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

 

Please mark as Best Answer so that it can help others in the future.

Regards,

Priya Ranjan

 

ANUTEJANUTEJ (Salesforce Developers) 
Hi Girish,

Map is a primitive data type that has key-value pairs and the keys need to be unique.

"Map keys and values can be of any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types.
Uniqueness of map keys of user-defined types is determined by the equals and hashCode methods, which you provide in your classes. Uniqueness of keys of all other non-primitive types, such as sObject keys, is determined by comparing the objects’ field values.
Map keys of type String are case-sensitive. Two keys that differ only by the case are considered unique and have corresponding distinct Map entries. Subsequently, the Map methods, including put, get, containsKey, and remove treat these keys as distinct."

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

As mentioned in the above link, "When working with SOQL queries, maps can be populated from the results returned by the SOQL query. The map key should be declared with an ID or String data type, and the map value should be declared as an sObject data type.

This example shows how to populate a new map from a query. In the example, the SOQL query returns a list of accounts with their Id and Name fields. The new operator uses the returned list of accounts to create a map."
// Populate map from SOQL query
Map<ID, Account> m = new Map<ID, Account>([SELECT Id, Name FROM Account LIMIT 10]);
// After populating the map, iterate through the map entries
for (ID idKey : m.keyset()) {
    Account a = m.get(idKey);
    System.debug(a);
}

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.