Salesforce Collections: Set, List

You can create a Set in Apex as below

Set<String> colorSet = new Set<String> {'Red', 'Blue', 'Orange'};

To create list, use the syntax below

List<String> colorList = new List<String> {'Brown', 'Black', 'Green', 'White'};

Convert from List to Set

Set<String> newColorSet= new Set<String>(colorList);

Convert from Set to List

List<String> newColorList = new List<String>(colorSet);

You can also use other ways like using addAll

colorList.addAll(colorSet);
colorSet.addAll(colorList);

Permanent link to this article: https://salesforcebuddy.com/2020/05/salesforce-collections-set-list/

Leave a Reply

Your email address will not be published.