Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code used in map advanced methods tutorial on SFDC Stop (https://youtu.be/km2gUwkBN78)
Map advanced methods:
----------------------
keySet(): Get all keys from a map
-------------------------------------
Map<Integer, String> employeesMap = new Map<Integer, String>{
1 => 'Richard',
2 => 'Monica',
3 => 'Erlich',
4 => 'Dinesh'
};
System.debug('Initial Map = ' + employeesMap);
Set<Integer> employeeIds = employeesMap.keySet();
System.debug(employeeIds);
values(): Get all values from a map
--------------------------------------
Map<Integer, String> employeesMap = new Map<Integer, String>{
1 => 'Richard',
2 => 'Monica',
3 => 'Erlich',
4 => 'Dinesh',
5 => 'Richard'
};
System.debug('Initial Map = ' + employeesMap);
List<String> employeeNames = employeesMap.values();
System.debug(employeeNames);
clear(): Remove all key-value pairs from a map
------------------------------------------------
Map<Integer, String> employeesMap = new Map<Integer, String>{
1 => 'Richard',
2 => 'Monica',
3 => 'Erlich',
4 => 'Dinesh',
5 => 'Richard'
};
System.debug('Initial Map = ' + employeesMap);
System.debug('Clearing all pairs.....');
employeesMap.clear();
System.debug('Final Map = ' + employeesMap);
isEmpty(): Check if a map is empty
------------------------------------
Map<Integer, String> employeesMap = new Map<Integer, String>{
1 => 'Richard',
2 => 'Monica',
3 => 'Erlich',
4 => 'Dinesh',
5 => 'Richard'
};
System.debug('Initial Map = ' + employeesMap);
System.debug('Is my map empty? => ' + employeesMap.isEmpty());
System.debug('Clearing all pairs.....');
employeesMap.clear();
System.debug('Final Map = ' + employeesMap);
System.debug('Is my map empty? => ' + employeesMap.isEmpty());
size(): Get total number of key value pairs from a map
-------------------------------------------------------
Map<Integer, String> employeesMap = new Map<Integer, String>{
1 => 'Richard',
2 => 'Monica',
3 => 'Erlich',
4 => 'Dinesh',
5 => 'Richard'
};
System.debug('Initial Map = ' + employeesMap);
System.debug('Size of the map = ' + employeesMap.size());
System.debug('Clearing all pairs.....');
employeesMap.clear();
System.debug('Final Map = ' + employeesMap);
System.debug('Size of the map = ' + employeesMap.size());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment