Nowadays, Salesforce methods have a variety of applications related to the grouping of data on the basis of data types. Few numbers of examples cover the usage of set methods in Salesforce where there is a list of methods that are responsible for wide applications of utilities. The Salesforce developers must know that the output of the SOQL Query is literally a list and that is the reason for emphasizing the list methods in Apex.
The prime motive of this digital piece is to emphasize on the several kinds of list methods in Salesforce and grab the utilities in the dynamic utilities. Through the information presented here, you will be enlighted with the usage of set methods in salesforce.
So without wasting any time let’s get to see what you are going to learn today:
1. Understanding set in Salesforce?
2. What are set methods in Salesforce?
3. Distinguish between list and set in Salesforce
4. What are types of set methods in Salesforce?
5. Discussion of the utilities of set methods in Salesforce
6. What is a method and describe its creation in Salesforce?
7. Parting tips
8. Some most asked questions and answers
Understanding set in Salesforce?
A set in Salesforce is a collection of different kinds of elements that possess different data types in an orderly manner. These data types can be primitive data types, user-defined data types, built-in apex data types, collections, etc.
The number of elements present in a set does not allow the duplication of a particular set of value. There is no provision for preserving the insertion sequence present within the set and also you are not permitted to perform DML.
Set elements dynamically develop and grow during the run time akin to a list. If you want to make the implication of a set you should initially try using the set keyword and then you have to enter the data type name of the primitive by using the special characters, <>.
You can use the set methods in Salesforce at the time you want. All in all, a set is an ordered combination of different data types that comprise the different counts/numbers of a definite order of the records or datasets.
What are set methods in Salesforce?
Set methods comprise a set class that is responsible for the gathering of unique elements where there is no permission for duplication of the values of the objects. The major application of set methods in Salesforce lies in the fact that they worked on the basis of a set which is an ordered group of elements being initialized with the usage of Set keyboard.
As we all know that the set elements can have a possession of a random or a specific kind of data type which can be used a defined or built-in. There is a second name of set methods are renown after instance methods that guides us in favor of operating on a particular method. Also, we need a set Constructor and set method for implementing the sets of different data types.
Distinguish between lists and set in Salesforce
The list in Salesforce is similar to a definite order of the elements which can be distinguished on the basis of their indices possessing a particular order/sequence. The data type can vary for the different elements inside the list. Different data types are primitive types, collections, built-in Apex, etc.
A set in Salesforce is a random combination of many elements where there is no replica of any element arranged according to a specific kind of sequence.
Various types of Collections
There are different types of collections given under.
1. List
The list is an order of the elements which can be distinguished on the basis of their indices. This order follows a definite pattern. The data type can vary for the different elements in the set inside the list. Different data types are primitive types, collections, built-in Apex, etc.
2. Sets
A set isa gathering of different kinds in an unordered manner of elements that possess different data types. These data types can be primitive data types, user-defined data types, built-in apex data types, collections, etc.
3. Maps
By following the definition of the map, it is definite/ordered collection of pairs of various key values. Every single key is regarded as a unique key that indicates a single value. Any kind of data type can be allotted to these values and keys. These data types can be primitive data types, user-defined data types, built-in apex data types, collections, etc.
4. Parameterized typing
In consideration of the methods in salesforce, Apex as a programming language that is static in nature. It is necessary that we need to indicate the type of data for a particular variable before its usage.
There are some free transaction limits to the apex that guides us about the limitations of the account before launching the transaction of Apex. Especially for batch Apex, week and make the resetting of all the limitations after every competition of a group in a particular record with the help of a method with the name is-Execute.
What are types of set methods in Salesforce?
There are several kinds of methods available in the set class. So, here are the list methods in Salesforce.
1. add(set Element)
2. addAll(SetElement)
3. clear()
4. clone()
5. add(index, setElement)
6. equals(set2)
7. removeAll(setOfElementsToRemove)
8. remove(setElement)
9. size()
10. addAll(from List)
Discussion of the utilities of set methods in Salesforce
The salesforce set methods are as follows-
1. add(set Element)
It is used for the insertion of an element into the set.
2. add(index, setElement)
It is helpful in adding all the elements present in a particular setlist. This is done so when these elements are not already present in the list of the set.
3. addAll(from List)
It is used for the addition of all the list of elements present in a particular set. The process of addition can be done after calling a particular method only after cross-checking their absence in the list of sets.
4. addAll(SetElement)
It is used for the addition of all the elements present in a particular set. The process of addition can be done after calling a distinctmethod after making sure that the element is already existing.
5. Clear()
It helps in the removal of all elements in the specified set.
The syntax for the methods clear() is public void clear().
The return value is of type void.
6. Clone()
It helps in the creation of a duplicate copy of the set.
The syntax is public set< object> clone()
The return value from the set method is type- Set(of the same type).
7. remove(setElement)
It helps in the removal of a particular element from the list of the set if it is already present.
8. Size()
It is used to estimate the size of the number of elements present in the specific set.
Also, it is able to count the numbers of elements in totality.
9. equals(set2)
This set method is used for the comparison of the set with another specific set and finally, it returns the output true in case both the sets are equals. Otherwise, it returns false.
10. hashCode()
It is responsible for returning the hash-code that corresponds to a particular set and also, to the contents present inside the set.
11. isEmpty() contains(element)
It returns the value true in case all the elements are zero in number inside the list of sets.
12. containsAll(setToCompare)
It is helpful in returning the true value in case of the set if containing all the due number of elements of a particular set.
The syntax is public Boolean contains (Object setElement)
The return value from the set method is type- Object.
The data type for the return value is – Boolean.
13. containsAll(listToCompare)
It is helpful in returning the true value in case of the list of set if containing all the required elements of a particular set list.
14. removeAll(setOfElementsToRemove)
It is helpful in removing or deleting the elements which are present in a specific set if there are already present inside the original set.
15. removeAll(setOfElementsToRemove)
It is helpful in removing or deleting the elements which are present in a specific list of set if there are already present inside the original set list.
What is a method and describe its creation in Salesforce?
Now, we shall discuss what is a set-in apex.
A method/set in Apex is defined as the collection of several statements that share some common characteristics and behaviour and therefore, they are grouped together so that they can perform a particular operation based on a query.
For the creation of the method in Apex set class, in general terms, we can create a Apex set method by following the text given below-
modifier returnValueType methodName(list of parameters) {
// Method body;
}
We should know that a method consists of several components that include the header and specifically a method body that contains the conditions for testing.
All the associated parts are given below which are comprised in a method of Apex list class during its creation of the body in Apex set.
1. Modifiers
This is an optional service that can be given to the method body and Guides the compiler of the way it calls a particular method. This is responsible for defining the access type of the method.
2. Return type
After you create a method it is probably a possibility that it may return a significant value.
The data type for the return type used under the method is- returnValueType.
This helps in performing the desired calculations in operations without caring about the output or the return of the method segment in the form of a value. When there is no value being returned the case becomes null and void and the keyword used is void for returnValueType.
3. Method name
This is the literal name of the method that displays the parameter lists being constituted together in the form of a method signature.
4. Parameters
A parameter is treated just like a placeholder in which a value can be passed into the parameter as soon as the method is invoked. We can call this parameter the actual parameter and the returned value is also called as an argument. The list of parameters defines the type, the order, and the total number of defined parameters in a particular method.
The process of giving parameters is optional and we can understand that it is not necessary that every time the following set method contain a parameter or parameters.
5. Method body
This is the main body that has the several numbers of statements being defined with relation to the specific method and the functionality of the method.
We need to follow the Syntax given below.
public static integermax(integer a1, integer a2) {
integer result;
if (a1 > a2)
result = a1;
else
result = a2;
return result;
}
Parting tips
Hope you have liked the information that has been presented above. We tried our best to make you understand about set opportunity in Salesforce and apply them in different respects.
So if you are preparing of being a salesforce professional, the Knowledge of set methods in salesforce is very important. In addition there are numerous questions based on salesforce set methods, every year in all salesforce exams. So preparing fro them is a must
FAQs
What are the different set constructors in Salesforce?
1. Set<T>()
It is helpful in the creation of a new instance for a particular set class. It can hold any number of elements of different data types.
2. Set<T>(setToCopy)
It is helpful in the creation of a dummy example by duplicating or copying the different elements of a particular set inside the set class. The data type used for such elements is T.
3. Set<T>(listToCopy)
It is helpful in the creation of a fresh example by duplicating or copying the different List elements of a particular set. The data type used for such List elements is T.