Sets Initialization And Methods
Chapter Topics
- Apex Data Types
- Integer Data Type in APEX
- Floating Point Data Type in APEX
- String Data Type in APEX
- Date Datatypes in APEX
- Time and DateTime Data Types in APEX
- Boolean Datatype in Salesforce
- ID & Blob Datatypes in APEX
- What is the Rule of Conversions in Apex?
- Enums in APEX
- sObjects in Salesforce
- Generic sObjects in Salesforce
- Collections in APEX
- Lists in APEX
- List Array Notation in APEX
- Lists Initialization in APEX
- Nested Lists in APEX
- Sets in APEX
- Sets Initialization And Methods
- Maps in APEX
- Map Initialization Methods in APEX
- Operators in Apex and Their Types
- Shorthand Operator
- Equality Operator
- Relational Operators(<,>,<=,>=)
What is Set Initialization in Apex?
In Salesforce Apex, initializing a Set can be done in several ways depending on whether you want the Set to be empty initially or pre-filled with values. The way you declare sets is similar to the way you declare lists. You can also have nested sets and sets of lists.
How to Initialize Set in Apex?
In Salesforce Apex, you can initialize a set in various ways. Here’s how you can do it:
// Empty set initialized Set<String> strSet = new Set<String>(); // Set of List of Strings Set<List<String>> set2 = new Set<List<String>>(); |
How to Use Set in Salesforce Apex?
Following are some common methods of set in Apex:
S.No | Function | Example |
1 | add(Element)Adds an element to set and only takes the argument of special datatype while declaring the set. | Set<String> s = new Set<String>();s.add(‘abc’);s.add(‘ABC’);s.add(‘abc’);System.debug(s);//(‘abc’,’ABC’) |
2 | addAll(list/set)Adds all of the elements in the specified list/set to the set if they are not already present. | List<String> l = new List<String>();l.add(‘abc’);l.add(‘def’);s.addAll(l);System.debug(s);//(‘abc’,’ABC’,’def’) |
3 | clear()Removes all the elements. | s.clear();s.addAll(l); |
4 | clone()Makes duplicate of a set. | List<String> s2 = s.clone();System.debug(s);//(‘abc’,’def’) |
5 | contains(elm)Returns true if the set contains the specified element. | Boolean result = s.contains(‘abc’);System.debug(result); // true |
6 | containsAll(list)Returns true if the set contains all of the elements in the specified list. The list must be of the same type as the set that calls the method. | Boolean result = s.containsAll(l);System.debug(result); // true |
7 | size()Returns the size of set. | System.debug(s.size()); // 2 |
8 | retainAll(list)Retains only the elements in this set that are contained in the specified list and removes all other elements. | s.add(‘ghi’);System.debug(s);//(‘abc’,’def’,’ghi’)s.retainAll(l);System.debug(s);//(‘abc’,’def’) |
9 | remove(elm)Removes the specified element from the set if it is present. | s.add(‘ghi’);s.remove(‘ghi’);System.debug(s);//(‘abc’,’def’) |
10 | removeAll(list)Removes the elements in the specified list from the set if they are present. | s.add(‘ghi’);s.removeAll(l);System.debug(s);//(‘ghi’) |
Need more support?
Get a head start with our FREE study notes!
Learn more and get all the answers you need at zero cost. Improve your skills using our detailed notes prepared by industry experts to help you excel.