function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
santhosh konathala 8santhosh konathala 8 

Can send the code for conversion of list to set?

Best Answer chosen by santhosh konathala 8
DeepthiDeepthi (Salesforce Developers) 
Hi Santhosh,

The simplest way to convert List to Set in Salesforce is given below:     
List<String> tempList = new List<String>();
Set<String> tempSet = new Set<String>();
tempList.add('One');
tempList.add('Two');
tempList.add('Three');  
tempSet.addAll(tempList);

tempList is List datatype and tempSet is Set datatype.
Add some values to List datatype and use addAll() to add it to Set datatype.

Hope this helps you!
Best Regards,
Deepthi

All Answers

DeepthiDeepthi (Salesforce Developers) 
Hi Santhosh,

The simplest way to convert List to Set in Salesforce is given below:     
List<String> tempList = new List<String>();
Set<String> tempSet = new Set<String>();
tempList.add('One');
tempList.add('Two');
tempList.add('Three');  
tempSet.addAll(tempList);

tempList is List datatype and tempSet is Set datatype.
Add some values to List datatype and use addAll() to add it to Set datatype.

Hope this helps you!
Best Regards,
Deepthi
This was selected as the best answer
Prem Anandh 1Prem Anandh 1
Hi Santhosh, 

Please see below code for your referene. 

List<String> lstString = new List<String>();
Set<String> setString = new Set<String>();

for(Integer i=0; i < 10; i++)
{
    lstString.add('Test'+i);
}

setString.addAll(lstString); //It will not allow duplicate values

Thanks,
Prem Anandh
santhosh konathala 8santhosh konathala 8
Hi have a doubt  set do not accepts duplicates then how can I  check the element is existed or not if  we converted list to set only unique  elements get stored.