Convert List to Set in Salesforce

Convert List to Set in Salesforce

To convert List to Set in Salesforce, add some values to List datatype and use addAll() to add it to Set datatype.

Sample Code:    

List < String > tempList = new List < String >();
Set < String > tempSet = new Set < String >();
tempList.add( 'One' );
tempList.add( 'Two' );
tempList.add( 'One' );  
System.debug(
    'tempList is' + tempList
);
tempSet.addAll( tempList ); 
System.debug(
    'tempSet is' + tempSet
);

tempList is List datatype and tempSet is Set datatype.

Convert List to Set in Salesforce
Convert List to Set in Salesforce

Leave a Reply