SFDC Stop - Always the latest about Salesforce


Full Tutorial Series with videos, free apps, live sessions, salesforce consulting and much more.


Telegram logo   Join our Telegram Channel
Showing posts with label Apex Collections. Show all posts
Showing posts with label Apex Collections. Show all posts

Tuesday, 6 August 2024

Collections in Apex - Salesforce Apex Tutorial Part 8 to 13 | Code Snippet

Hello Trailblazers,


In this post, I'm sharing the code snippet for Coding with Confidence: The Fun Way to Learn Salesforce Apex tutorial series part 8 and above below:
// * Collections in Apex

/*
*   There are 3 types of collections in apex:
*   1. List
*   2. Set
*   3. Map
*/

// * List - Part 1

// * List: A list is an ordered collection of elements. We can create a list of any data type like: primitive data types, collections, sObjects, custom data types etc.

// * Creating a list of names
List<String> names = new List<String>();
names.add('Richard Hendricks');
names.add('Monica');
names.add('Erlich Bachman');
names.add('Dinesh');
names.add('Gilfoyle');

System.debug('Names = ' + names);

// * Retrieving an element from the list
System.debug('Element at index 2 = ' + names[2]);

// * Replacing a list element at a particular index
names.set(2, 'Gavin Belson');
System.debug('Updated Names = ' + names);

names[2] = 'Erlich Bachman';
System.debug('Updated Names = ' + names);

// * Setting an index that doesn't exist
// names.set(10, 'Jared Dunn');

// names[10] = 'Jared Dunn';

// * Displaying the size of the list
System.debug('Size of list "names" = ' + names.size());

// * Clearing the list
names.clear();

System.debug('Size of list "names" = ' + names.size());
System.debug('Updated Names = ' + names);

// * List - Part 2

// * Checking if the list is empty
List<Integer> numbers = new List<Integer>();
System.debug('Numbers List = ' + numbers);
System.debug('Numbers List is empty = ' + numbers.isEmpty());

numbers.add(1);
numbers.add(2);

System.debug('Numbers List = ' + numbers);
System.debug('Numbers List is empty = ' + numbers.isEmpty());

// * Defining lists using array notation
String[] names = new String[5];
// List<String> names = new String[5];
// String[] names = new List<String>();
names[2] = 'Gavin Belson';
names[4] = 'Jared Dunn';
names[0] = 'Richard Hendricks';
names[1] = 'Erlich Bachman';
names[3] = 'Monica';
// names.add('Gavin Belson');
System.debug('Names Array = ' + names);
System.debug('Size of list "names" = ' + names.size());

// names[5] = 'Dinesh Chugtai';

names.add('Dinesh Chugtai');

System.debug('Updated Names = ' + names);
System.debug('Size of list "names" = ' + names.size());

// * Populating values inside a list at the time of initialization
List<String> names = new List<String>{'SFDC Stop', 'Rahul Malhotra'};
System.debug('Initial Names = ' + names);

String[] names = new String[]{'SFDC Stop', 'Rahul Malhotra'};
System.debug('Initial Names = ' + names);

// * List - Part 3

// * Sorting a list
List<String> names = new List<String>{'Richard Hendricks', 'Erlich Bachman', 'Gavin Belson', 'Monica', 'Jared Dunn', ''};
System.debug('Names = ' + names);
names.sort();
System.debug('Names in ascending order = ' + names);

// * Creating a list of collections
List<List<String>> names = new List<List<String>>(); // * Upto 8 levels
names.add(new List<String>{'Aman', 'Ashish', 'Adam', 'Anika'});
names.add(new List<String>{'Bob', 'Bhanu', 'Blake', 'Benjamin'});
System.debug('Names = ' + names);
System.debug('Names starting with "A" = ' + names[0]);
System.debug('Names starting with "B" = ' + names[1]);
System.debug('2nd name starting with "A" = ' + names[0][1]);
System.debug('3rd name starting with "B" = ' + names[1][2]);

// * Set

// * Set: A set is an unordered collection of elements that do not contain any duplicates. We can create a set of any data type like: primitive data types, collections, sObjects, custom data types etc.

// * Creating a set of integers
Set<Integer> numbers = new Set<Integer>();
numbers.add(2);
numbers.add(1);
numbers.add(4);
numbers.add(5);
numbers.add(3);

System.debug('Numbers Set = ' + numbers);
System.debug('Number of elements in Set = ' + numbers.size());

// * Adding duplicate elements inside set
numbers.add(1);
numbers.add(5);

System.debug('Numbers Set after adding duplicate elements = ' + numbers);
System.debug('Number of elements in Set = ' + numbers.size());

// * Check if 3 is present in set
System.debug('Is 3 present inside set = ' + numbers.contains(3));

// * Removing 3 from set
System.debug('Removing 3 from set...');
numbers.remove(3);

// * Check if 3 is present in set
System.debug('Is 3 present inside set = ' + numbers.contains(3));

System.debug('Numbers Set after removing 3 = ' + numbers);
System.debug('Number of elements in Set = ' + numbers.size());

// * Populating values inside a set at the time of initialization
Set<String> fruits = new Set<String>{'Mango', 'Apple', 'Banana', 'Pear', 'Mango'};
System.debug('Elements inside set of fruits = ' + fruits);

// * Map - Part 1

// * Map: A map is a collection of key-value pairs where each unique key maps to a single value. Keys and Values inside a map can be of any data type like: primitive data types, collections, sObjects, custom data types etc.

// * Creating a map of Integer, String:

Map<Integer, String> studentsMap = new Map<Integer, String>();
studentsMap.put(3, 'Gilfoyle');
studentsMap.put(5, 'Monica');
studentsMap.put(1, 'Richard Hendricks');
studentsMap.put(2, 'Dinesh');
studentsMap.put(4, 'Erlich Bachman');
studentsMap.put(null, 'Jared Dunn');

System.debug('Value of studentsMap = ' + studentsMap);
System.debug('Total number of elements inside studentsMap = ' + studentsMap.size());

// * Getting value from a map
System.debug('Student with roll number 3 = ' + studentsMap.get(3));

// * Updating value inside a map
System.debug('Assigning a new name to roll number 3');
studentsMap.put(3, 'Gavin Belson');

System.debug('Student with roll number 3 = ' + studentsMap.get(3));
System.debug('Value of studentsMap = ' + studentsMap);

// * Removing value from a map
studentsMap.remove(3);
System.debug('Value of studentsMap after removing student with roll number 3 = ' + studentsMap);
System.debug('Total number of elements inside studentsMap = ' + studentsMap.size());

// * Map - Part 2

Map<Integer, String> studentsMap = new Map<Integer, String>();
studentsMap.put(5, 'Monica');
studentsMap.put(1, 'Richard Hendricks');
studentsMap.put(2, 'Dinesh');
studentsMap.put(4, 'Erlich Bachman');
studentsMap.put(null, 'Jared Dunn');

// * Check if the map contains a key
System.debug('Does studentsMap contain roll number 4? ' + studentsMap.containsKey(4));
System.debug('Does studentsMap contain roll number 3? ' + studentsMap.containsKey(3));

System.debug('Value of studentsMap = ' + studentsMap);
System.debug('Total number of elements inside studentsMap = ' + studentsMap.size());

// * Check if map is empty
System.debug('Is student map empty? ' + studentsMap.isEmpty());

// * Remove all elements from the map
System.debug('Clearing elements inside studentsMap...');
studentsMap.clear();

// * Check if map is empty
System.debug('Is student map empty? ' + studentsMap.isEmpty());

System.debug('Value of studentsMap = ' + studentsMap);
System.debug('Total number of elements inside studentsMap = ' + studentsMap.size());

// * Getting KeySet and Values from the map
Map<Integer, String> studentsMap = new Map<Integer, String>();
studentsMap.put(1, 'Richard Hendricks');
studentsMap.put(2, 'Dinesh');
studentsMap.put(3, 'Gilfoyle');
studentsMap.put(4, 'Erlich Bachman');
studentsMap.put(2, 'Monica');
studentsMap.put(6, 'Dinesh');

System.debug('Value of studentsMap = ' + studentsMap);
System.debug('Total number of elements inside studentsMap = ' + studentsMap.size());

Set<Integer> keys = studentsMap.keySet();
System.debug('Keys of studentsMap = ' + keys);

List<String> studentNames = studentsMap.values();
System.debug('Values of studentsMap = ' + studentNames);

YouTube Tutorials

You can check out the tutorials below as well:

Apex Lists: Master List Manipulation - Part 1 | Apex Collections - Salesforce Apex Tutorial Part 8

Apex Lists: Master List Manipulation - Part 2 | Apex Collections - Salesforce Apex Tutorial Part 9


Apex Lists: Master List Manipulation - Part 3 | Apex Collections - Salesforce Apex Tutorial Part 10

Master Apex Sets: A Concise Guide | Apex Collections - Salesforce Apex Tutorial Part 11

Unlock the power of maps in Apex! 🗺️ - Part 1 | Apex Collections - Salesforce Apex Tutorial Part 12

Unlock the power of maps in Apex! 🗺️ - Part 2 | Apex Collections - Salesforce Apex Tutorial Part 13


Check out the tutorials and let me know your feedback in the comments down below.

Happy Trailblazing!!