Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
22 views8 pages

Apex Collections With Examples

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views8 pages

Apex Collections With Examples

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Nripesh Kumar Joshi (Salesforce Developer)

Collection(List, Set, Map) in Apex

List --> It is basically ordered list and icontain duplicate values and we can access the element
using index.

Example --> There are different ways of adding the value in List

Syntax of creating a list in apex

List<Integer> exampleList = new List<Integer>();


List<String> exampleList = new List<String>{‘a’,’b’,’c’};

A). Add the element in list with simple way

Exmple of List of integer

List<Integer> intList = new List<Integer>{1, 2,3,4,5};


system.debug('Integer List'+ intList);

Exmple of List of String

List<String> strList = new List<String>{'Sales Cloud', 'Service Cloud', 'Experience Cloud', 'Data
Cloud', 'Financial Service Cloud'};
system.debug('String List'+ strList);

B). Adding element in last using add function


Exmple of List of integer

List<Integer> intList = new List<Integer>();


intList.add(1);
intList.add(2);
intList.add(3);
intList.add(4);
intList.add(5);

system.debug('Integer List'+ intList);

Output ---->

Some methods that can be used in list

1. add(listElement): Adds an element to the end of the list.

List<String> strList = new List<String>();


strList.add('Sales Cloud');
strList.add('Service Cloud');
strList.add('Data Cloud');
strList.add('AI Cloud');
strList.add('Experience Cloud');
system.debug('String List'+ strList);

2. add(index, listElement): Inserts an element into the list at the specified index position.

List<Integer> intList = new List<Integer>{1,2,3,4,5};


intList.add(3,0);
system.debug('Integer List'+ intList);

3. contains(listElement): Returns true if the list contains the specified element.

List<String> strList = new List<String>{'Sales Cloud', 'Service Cloud', 'Experience Cloud', 'Data
Cloud', 'Financial Service Cloud'};
Boolean strContain = strList.contains('Sales Cloud');
system.debug('String List'+ strContain);
4. size(): Returns the number of elements in the list.

List<String> strList = new List<String>{'Sales Cloud', 'Service Cloud', 'Experience Cloud', 'Data
Cloud', 'Financial Service Cloud'};
system.debug('Return List size >>>> '+ strList.size());

5. isEmpty(): Returns true if the list has zero elements.

List<String> strList = new List<String>{'Sales Cloud', 'Service Cloud', 'Experience Cloud', 'Data
Cloud', 'Financial Service Cloud'};
system.debug('Return true of list is empty otherwise false >>>> '+ strList.isEmpty());

6.indexOf(listElement): Returns the index of the first occurrence of the specified element in this
list.

List<String> strList = new List<String>{'Sales Cloud', 'Service Cloud', 'Experience Cloud', 'Data
Cloud', 'Financial Service Cloud'};
system.debug('String>>>> '+ strList.indexOf('Data Cloud'));

If this list does not contain the element, returns -1.

List<String> strList = new List<String>{'Sales Cloud', 'Service Cloud', 'Experience Cloud', 'Data
Cloud', 'Financial Service Cloud'};
system.debug('String>>>> '+ strList.indexOf('Cloud'));
SET – It is an unordered collection and it does not contain duplicate value

SET<String> exampleSet = new SET<String>();


exampleSet.add('a');
exampleSet.add('b');
exampleSet.add('c');
system.debug('Set>>>> '+ exampleSet);

if you will try to set the duplicate value, it will not add the duplicate value in set. In the below
example you can see that duplicate value is not adding in set.

SET<String> exampleSet = new SET<String>();


exampleSet.add('a');
exampleSet.add('b');
exampleSet.add('c');
exampleSet.add('c');
system.debug('Set>>>> '+ exampleSet);

Set does not support the indexof(element) but we can access by value. All methods are same as list
in SET.

Map - A map is a more complex collection than either a list or a set.


Each item in a map has two parts: a key and a value, known as a key-value pair. Keys and values
can be any data type. Although each key is unique, values can be repeated within a map

** Declare a Map Using the Map Keyword **

Map<String, Integer> myMap = new Map<String, Integer>();


myMap.put('One', 1);
myMap.put('Two', 2);
system.debug('Map>>>>'+myMap);
In this map, Key is the String and Integer is Value.

** Declare and Initialize a Map with Values **

Map<String, String> countryCapitalMap = new Map<String, String>{


'USA' => 'Washington, D.C.',
'India' => 'New Delhi',
'Japan' => 'Tokyo'
};
system.debug('countryCapitalMap>>>>'+countryCapitalMap);

Here, countryCapitalMap is a map with string keys and string values, and it is initialized with key-
value pairs.

USA,India, Japan are key and Washington, D.C, New Delhi and Tokyo are values

** Declare a Map with a Specific Type Using Sobject **

Map<Id, Account> accountMap = new Map<Id, Account>();


List<Account> accounts = [SELECT Id, Name FROM Account LIMIT 5];
for (Account acc : accounts) {
accountMap.put(acc.Id, acc);
}
system.debug('accountMap>>>>'+accountMap);

In this case, accountMap is a map with Salesforce record IDs as keys and Account objects as values.

Example of map and explain the different methods

1. get(key): Retrieves the value associated with the specified key.

Map<String, String> countryCapitalMap = new Map<String, String>{


'USA' => 'Washington, D.C.',
'India' => 'New Delhi',
'Japan' => 'Tokyo'
};
// Get the New Delhi value so we have to get the value using India Key

String value = countryCapitalMap.get('India');


system.debug('countryCapitalMap value>>>>'+value);

2. containsKey(key): Checks if the map contains a specific key.

Boolean value = countryCapitalMap.containsKey('India');


Boolean val = countryCapitalMap.containsKey('UK');
system.debug('countryCapitalMap value 1>>>>'+value);
system.debug('countryCapitalMap val>>>>'+val);

3. keySet(): Returns a set of all the keys in the map.

Set<String> keys = countryCapitalMap.keySet();


system.debug('keys >>>>'+keys);

India, Japan, USA are keys

4. values(): Returns a list of all the values in the map.

List<String> allValues = countryCapitalMap.values();


system.debug('allValues >>>>'+allValues);
How to iterate on map.values()
// Declare a Map with a Specific Type Using SObject:
Map<Id, Account> accountMap = new Map<Id, Account>();
List<Account> accounts = [SELECT Id, Name FROM Account LIMIT 5];
for(Account acc : accounts) {
accountMap.put(acc.Id, acc);
}

for(Account acc: accountMap.values()){


system.debug('acc >>>>'+acc.Name);

In this example are iterating over the value of accountMap and In above pic We can see the values
of AccountMap (Account Name)

How to iterate on map.keyset()

Map<Id, Account> accountMap = new Map<Id, Account>();


List<Account> accounts = [SELECT Id, Name FROM Account LIMIT 5];
for(Account acc : accounts) {
accountMap.put(acc.Id, acc);
}
for(Id acc: accountMap.KeySet()){
system.debug('acc >>>>'+acc);

}
In the above pic you can see the key means (Id) as per our map

Difference between List, Set and Map

Feature Map Set List


Unordered collection of Ordered collection of
Definition Collection of key-value pairs
unique elements elements
Map<KeyType, Set<ElementType> List<ElementType>
ValueType> mapName = setName = new listName = new
Syntax new Map<KeyType, Set<ElementType>() List<ElementType>(
ValueType>(); ; );
Access by Access by index (zero-
Access by key Not applicable
Index/Key based)
Order No guaranteed order No guaranteed order Maintains insertion order
Unique keys; values may be May contain duplicate
Duplicates Unique elements
duplicates elements
Map<String, Integer> Set<String> mySet List<String>
myMap = new = new myList = new
Example Map<String, Set<String>{'apple t<String>{'apple',
Integer>(); ', 'orange', 'orange',
myMap.put('One', 1); 'banana'}; 'banana'};

You might also like