Linux Hint Posted July 25, 2023 Share Posted July 25, 2023 While executing the Apex transactional statements, you may encounter the duplicate records in your Salesforce objects. Records without duplicates are helpful for an SOQL query to fetch the records more. This can be possible by utilizing the set DataStructure instead of list in these scenarios. In this guide, we will discuss how to create a set in Apex and the available different methods in the set. Set Creation Generally, a set don’t allow duplicates and it is an unordered collection of items/elements. By utilizing the “Set” class, we create the set by specifying the variable name. We will discuss how to declare a Salesforce object with the set type while discussing the set methods in this guide. Syntax: Set<DataType> variable_name = new Set<DataType>(); Three scenarios can be possible to create a set. One is creating a set from the elements/items or from the sObject directly. We can create a new Apex set from the existing set of elements and existing list of elements. Example 1: Creating a Set We create two sets with “Integer” and “String” types and display the elements that are present in these two sets. // Create set of Type - Integer that hold duplicate ratings Set<Integer> ratings = new Set<Integer> {4,3,2,3,4,5,1,2,3}; system.debug('Ratings: '+ratings); // Create set that hold three items of Type - String Set<String> framework = new Set<String> {'Apex','LWC','Aura'}; system.debug('Frameworks: '+framework); Output: The first set holds duplicate entries. A set won’t allow them. So, unique ratings are stored and returned. The second set don’t include duplicates. So, all are allowed in the set named “framework”. Example 2: Creating a Set from the Existing Set Consider a set (framework1) with three strings. Declare another set (framework2) by assigning the first set to this set. // Create set that hold three items of Type - String Set<String> framework1 = new Set<String> {'Apex','LWC','Aura'}; system.debug('Frameworks 1: '+framework1); // Create framework2 from framework1 Set<String> framework2 = new Set<String> (framework1); system.debug('Frameworks 2: '+framework2); Output: Now, the second set (framework2) holds all the elements of the first set (framework1). Example 3: Creating a Set from the Existing List Consider a list (list1) with six strings that include three duplicates. Declare another set (set1) by assigning the list to the set. // Create List that hold six items (with duplicates) of Type - String List<String> list1 = new List<String> {'Apex','LWC','Aura','Apex','LWC','Aura'}; system.debug('List of Frameworks: '+list1); // Create Set - set1 from List - list1 Set<String> set1 = new Set<String> (list1); system.debug('Set of Frameworks: '+set1); Output: Now, the set holds only the unique elements of the list. Adding Elements to the Set It is possible to add single/multiple elements at a time to the set. The sources can be like list or set. We will discuss the methods that add the element/s to the set with examples. Example 1: Add a Single Element In Apex set, the add() method is used to add a single item to the set. If we pass the element that already exists in the set, it won’t be added. Syntax: input_set.add(element) Let’s create a set with three products and try to add two products to it. // Create set with some products Set set1 = new Set{'Books related','Kitchen items','Furniture'}; system.debug('Actual Set: '+set1); // Add 2 items to the Set set1.add('Electronic gadgets'); set1.add('Furniture'); system.debug('Final Set: '+set1); Output: Previously, the total number of elements in the set is 3. We add the “Electronic gadgets” and “Furniture” to the set. The “Furniture” is not added to the set as it already exists in the set. Now, the set holds four elements. Example 2: Add Multiple Elements The addAll() method takes the Apex set/list as a parameter such that all elements are added to the existing set. The elements are not added if they are contained in the existing set already. Syntax: Adding the list items: input_set.addAll(List_name) Adding the set items: input_set.addAll(Set_name) Let’s create a set (set1) and list (list1) with three products. Create two sets (new_set1, new_set2) and pass a set of values to the new_set1 and a list of values to the new_set2. // Create set with some products Set<String> set1 = new Set<String>{'Books related','Kitchen items','Furniture'}; system.debug('Actual Set: '+set1); // Create list with some products List<String> list1 = new List<String>{'Books related','Kitchen items','Kitchen items'}; system.debug('Actual List: '+list1); // Create two empty sets. Set<String> new_set1 = new Set<String>(); Set<String> new_set2 = new Set<String>(); // Add set1 to the new_set1 new_set1.addAll(set1); system.debug('new_set1: '+new_set1); // Add list1 to the new_set2 new_set2.addAll(list1); system.debug('new_set2: '+new_set2); Output: The new_set1 holds three items which are created from the set (set1). The new_set2 holds only two items which are created from the list (list1) since one value is duplicated in the list. Removing Elements from the Set We can delete the elements from the Apex set using the remove() and removeAll() methods. Example 1: Remove a Single Element In Apex set, the remove() method is used to remove only the single element that is provided to it as a parameter. Syntax: input_set.remove(element) Let’s utilize the products set and remove “Furniture” from it. // Create set with some products Set<String> set1 = new Set<String>{'Books related','Kitchen items','Furniture'}; system.debug('Actual Set: '+set1); // remove Furniture from set1 set1.remove('Furniture'); system.debug('Final Set: '+set1); Output: After removing “Furniture”, the set holds the “Books related” and “Kitchen” items. Example 2: Remove Multiple Elements The removeAll() method takes the Apex set/list as a parameter such that the specified elements in the list/set are deleted from the existing set. Syntax: Removing items from a set that exists in the list: input_set.removeAll(List_name) Removing items from a set that exists in the other set: input_set.removeAll(Set_name) Let’s create a set (set1) and list (list1) with three products. Remove the elements from set1 that are in list1. // Create set with some products Set<String> set1 = new Set<String>{'Books related','Kitchen items','Furniture'}; system.debug('Actual Set: '+set1); // Create list with some products List<String> list1 = new List<String>{'Books related','Kitchen items'}; system.debug('Actual List: '+list1); // Remove items from set1 set1.removeAll(list1); system.debug('Final Set: '+set1); Output: Finally, the set holds only one item – “Furniture”. Other Methods Example 1: Contains() In Apex set, use the contains() method if you want to check whether the given element is present in the set. The “Method does not exist or incorrect signature” is raised if the wrong type is provided to this method. If an element is found, true is returned. Otherwise, false is returned. // Create set with some integers Set<Integer> student_marks = new Set<Integer>{90,89,70,80,50,81,70}; system.debug('Set: '+student_marks); // contains() system.debug(student_marks.contains(40)); system.debug(student_marks.contains(60)); system.debug(student_marks.contains(80)); Output: Example 2: The size() returns the total number of elements that are present in the set. The isEmpty() checks if the srt is empty or not. If it is empty, the Boolean value true is returned. Otherwise, false is returned. The equals() method returns true if all the elements in both sets are equal. Otherwise, false is returned. // Create two sets with some integers Set<Integer> student_marks1 = new Set<Integer>{90,89,70,80,50,81,70}; Set<Integer> student_marks2 = new Set<Integer>{90,89,70,80,50,81,70}; // Check both sets are equal or not system.debug(student_marks1.equals(student_marks2)); // Check if student_marks1 is empty or not system.debug(student_marks1.isEmpty()); // return the size of student_marks1 system.debug(student_marks1.size()); Output: Conclusion We learned how to utilize the set in Apex with simple examples. By utilizing the “Set” class, we create a set by specifying the variable name. A set can also be created from the list/set of elements. It won’t generate errors if you pass the duplicate elements, but these duplicates won’t be added to the set. View the full article Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.