Apex Collections
In Apex, collections are used to store multiple values in a single variable. They are similar to arrays but more powerful and flexible. There are three main types of collections in Apex:
• List – Ordered collection (allows duplicates)
• Set – Unordered collection (no duplicates)
• Map – Key-value pair collection
1. List
A List stores values in the order you insert them and allows duplicate values.
Syntax:
List<DataType> listName = new List<DataType>();
Example:
List<String> fruits = new List<String>();
fruits.add(‘Apple’);
fruits.add(‘Banana’);
fruits.add(‘Apple’); // duplicates allowed
System.debug(fruits);
Code Explanation:
• List<String>: A list that stores strings.
• fruits.add(…): Adds values to the list.
• Duplicate “Apple” is allowed.
• System.debug(fruits): Prints the whole list.
Output:
(Apple, Banana, Apple)
2. Set
A Set is like a bag that won’t hold duplicate items — it only keeps unique elements.
Syntax:
Set<DataType> setName = new Set<DataType>();
Example:
Set<String> cities = new Set<String>();
cities.add(‘Delhi’);
cities.add(‘Mumbai’);
cities.add(‘Delhi’); // Duplicate – will not be added
System.debug(cities);
Code Explanation:
• Set<String>: A set that holds strings.
• Adding ‘India’ twice, but it only stores one copy.
• System.debug(countries): Prints the set.
Output
{India, USA}
3. Map
A Map works like a dictionary — it stores a key and a value. You use the key to get the value.
Syntax:
Map<KeyType, ValueType> mapName = new Map<KeyType, ValueType>();
Example:
Map<Integer, String> studentNames = new Map<Integer, String>();
studentNames.put(1, ‘Adnan’);
studentNames.put(2, ‘Faiz’);
System.debug(studentNames);
Code Explanation:
• Map<Integer, String>: Key = Integer (ID), Value = String (Name).
• .put(…): Adds entries to the map.
• System.debug(…): Prints all entries.
Output
{1=Adnan, 2=Faiz}
Code Example
1: Working with List
List<Integer> numbers = new List<Integer>{10, 20, 30};
numbers.add(40);
System.debug(‘List Size: ‘ + numbers.size());
System.debug(‘First Element: ‘ + numbers[0]);
Code Explanation:
• A list is initialized with 3 numbers.
• numbers.add(40): Adds 40 to the list.
• size(): Tells how many items are in the list.
• numbers[0]: Gets the first element (10).
Output
List Size: 4
First Element: 10
2: Set Automatically Removes Duplicates
Set<String> emails = new Set<String>{‘a@test.com’, ‘b@test.com’, ‘a@test.com’};
System.debug(emails);
Code Explanation:
• Even though ‘a@test.com’ is added twice, Set only keeps unique entries.
Output
{a@test.com, b@test.com}
3: Map Lookup
Map<Integer, String> students = new Map<Integer, String>{ 1 => ‘Ali’, 2 => ‘Sara’, 3 => ‘Ahmed’ };
for (Integer id : students.keySet()) {
System.debug(‘ID: ‘ + id + ‘ | Name: ‘ + students.get(id));
}
Code Explanation:
• Adds countries and their capitals.
• .get(‘India’): Retrieves the capital of India.
• System.debug(…): Prints the capital.
Output
Capital of India:Delhi
4: Looping Over Map Keys and Values
Map<Integer, String> students = new Map<Integer, String>{
1 => ‘Ali’,
2 => ‘Sara’,
3 => ‘Ahmed’
};
for (Integer id : students.keySet()) {
System.debug(‘ID: ‘ + id + ‘ | Name: ‘ + students.get(id));
}
Code Explanation:
• Map is initialized with student IDs and names.
• keySet(): Returns all keys.
• Loop runs through each key and prints the corresponding value.
Output
ID: 1 | Name: Ali
ID: 2 | Name: Sara
ID: 3 | Name: Ahmed
Tasks For Practice
Task 1: Create a List of your top 5 favorite foods and print them using a loop.
Task 2: Create a Set of at least 6 animal names. Try to add a duplicate and print the result.
Task 3: Create a Map of country and capital (5 entries). Print all the country names and their capitals using a loop.
Task 4: Create a List of marks. Calculate and print the total using a loop.
Task 5: Use a Map to store student IDs and their marks. Print students who scored more than 80.
Task 6: Create a List to store 5 student names.
• Add names to the list
• Loop through the list using a for loop and print each name.
Task 7: Create a List with the following values: [1, 2, 3, 2, 4, 3, 5]
• Convert it into a Set<Integer> to remove duplicates
• Print the unique values.
Task 8: Create a Map to store employee names and their departments. Example:
‘Adnan’ → ‘IT’, ‘Sara’ → ‘HR’, ‘Zain’ → ‘Finance’