Optmizing Salesforce Development
Collection Data Type
In Apex
By Rajat .C. Modi
By Rajat .C. Modi
𝐖𝐡𝐚𝐭 𝐢𝐬 collection data type
A collection data type is a way to group
multiple items together in programming. It
helps you manage and organize data more
easily. In Apex, there are three main types of
collections:
1. List
2. Set
3. Map
By Rajat .C. Modi
List
An ordered collection that can contain duplicates.
Use it when you need to maintain the order of
elements or allow duplicates.For example, storing a
list of contacts.
List<String> names = new List<String>();
names.add('Alice');
names.add('Bob');
By Rajat .C. Modi
Set
An unordered collection that does not allow
duplicates. Use it when you want to ensure
uniqueness of elements. For example, storing
unique IDs.
Set<String> uniqueIds = new Set<String>();
uniqueIds.add('001');
uniqueIds.add('002');
By Rajat .C. Modi
Map
A collection of key-value pairs, where each key is
unique. Use it when you need to associate unique
keys with specific values. For example, mapping
account IDs to account names.
Map<String, String> accountMap = new Map<String,
String>();
accountMap.put('001', 'Acme Corp');
accountMap.put('002', 'Globex Corp');
By Rajat .C. Modi
Summary
List: Ordered, allows duplicates.
Set: Unordered, no duplicates.
Map: Key-value pairs, unique keys.
Choose the appropriate type based on the
requirements of your application.