You need to sign in to do that
Don't have an account?
ashish jadhav 9
How can I add list values into set?
Suppose I've a list of fruits like
List<string> fruits = new List<string> {'mango','papaya','orange','mango'};
then how can I assign this values to set?
for(integer i =0; i<fruits.size(); i++)
{
set<string> fruits1 = fruits[i];
}
The above code declaration is correct?
List<string> fruits = new List<string> {'mango','papaya','orange','mango'};
then how can I assign this values to set?
for(integer i =0; i<fruits.size(); i++)
{
set<string> fruits1 = fruits[i];
}
The above code declaration is correct?
Above code will help you. More details
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_set.htm#apex_System_Set_ctor
Once you got a List, just iterate it in a for-each loop and use .add method to add the elements in the set. For example you can add the elements of your List "Fruits" to set as :
set<String> setofFruits = new set<String>();
List<String> fruits = new List<String> {'mango','papaya','orange','mango'};
for(String str :fruits){
setofFruits.add(str);
}
To know more about set :
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_set.htm
Regards,
Ajay
You can assign the value of list to set by using predefine methods of Set.
Please let me know if this helps you!
Learn From ver basics how to add values to Apex Collection : List
- https://www.salesforcekid.com/2019/04/salesforce-apex-collection-list.html
Happy Learning ☁️⚡️