Activity › Forums › Salesforce® Discussions › What are the different types of collections and what are maps in Salesforce Apex?
Tagged: Collection Variable in Salesforce, Governor Limits, Lists, Maps in Salesforce, Salesforce Apex, Salesforce Fields, Salesforce Records, Salesforce sObject, Salesforce Variable, Transaction in Salesforce
-
What are the different types of collections and what are maps in Salesforce Apex?
Posted by Aman on September 22, 2018 at 10:51 AMWhat are the different types of collections and what are maps in Salesforce Apex?
Parul replied 7 years, 7 months ago 3 Members · 2 Replies -
2 Replies
-
Collections are the type of variables which can be used to store multiple number of records (data).
It is useful because Governor Limits restrict the number of records you can retrieve per transaction. Hence, collections can be used to store multiple records in a single variable defined as type collection and by retrieving data in the form of collections, Governor Limits will be in check. Collections are similar to how arrays work.
There are 3 collection types in Salesforce:
Lists
Maps
Sets
Maps are used to store data in the form of key-value pairs, where each unique key maps to a single value.
Syntax: Map<String, String> country_city = new Map<String, String>(); - [adinserter block='9']
-
The collection is the type variables which can store multiple numbers of records. It can increase and decrease dynamically.
There are 3 types of collection
List Collection
Set Collection
Map CollectionA map is a collection of key-value pairs where each unique key maps to a single value. Keys and values can be any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types.
Example:
public static void beforeUpdateHandler(Map<Id, Account> oldMap, Map<Id, Account> newMap) {
Account oldAcc;
Account newAcc;
for (Id accId : oldMap.keySet()) {
oldAcc = oldMap.get(accId);
newAcc = newMap.get(accId);
if (oldAcc.MyCustomField__c != newAcc.MyCustomField__c) {
System.debug(‘MyCustomField__c has changed!’);
}
}
}Thanks
Log In to reply.