Created
November 8, 2021 13:41
-
-
Save rahulmalhotra/f6f981097b0be10f41e1a2ef5bc08b09 to your computer and use it in GitHub Desktop.
Code used in set basics apex tutorial on SFDC Stop (https://youtu.be/ENkGqx03sOY)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Set Data Structure in Apex | |
------------------------------- | |
Def: A set is an unordered collection of elements that do not contain any duplicates. | |
1. Creating a set of Integers | |
Set<Integer> numbers = new Set<Integer>(); | |
System.debug(numbers); | |
Set<Integer> numbers = new Set<Integer>{30,30,30,20,20,10}; | |
System.debug(numbers); | |
2. Creating a set of Strings | |
Set<String> employees = new Set<String>{'Richard', 'Monica', 'Dinesh', 'Richard'}; | |
System.debug(employees); | |
3. Adding elements to the set | |
Set<Integer> numbers = new Set<Integer>(); | |
numbers.add(30); | |
numbers.add(30); | |
numbers.add(30); | |
numbers.add(20); | |
numbers.add(20); | |
numbers.add(10); | |
numbers.add(10); | |
System.debug(numbers); | |
4. Accessing elements from the set | |
Set<Integer> numbers = new Set<Integer>{30,30,30,20,20,10}; | |
System.debug(numbers); | |
for(Integer num : numbers) { | |
System.debug(num); | |
} | |
5. Removing element from the set | |
Set<Integer> numbers = new Set<Integer>{30,30,30,20,20,10}; | |
System.debug(numbers); | |
System.debug('Removing 20....'); | |
numbers.remove(20); | |
System.debug(numbers); | |
6. Checking if the element is present in the set or not | |
Set<Integer> numbers = new Set<Integer>{30,30,30,20,20,10,40,40}; | |
System.debug(numbers); | |
System.debug('Is 20 present inside the set? -> ' + numbers.contains(20)); | |
System.debug('Is 100 present inside the set? -> ' + numbers.contains(100)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment