Convert List to Set in Salesforce

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.


Cheers!!!

3 comments:

  1. Why not

    Set tempSet = new Set(tempList )

    ?

    I think that would be really the simplest way.

    ReplyDelete
    Replies
    1. This is simple. But above is another way.

      Cheers!!!

      Delete
  2. patlatus are yew freakin crazyy...

    ReplyDelete