+ Start a Discussion
Raj R.Raj R. 

How to add LIST of objects to a SET?

Hi,
I was wondering which method is closer to best practice:


List<CampaignMember> members = [Select Id, Status From CampaignMember Where Status='Registered'];
Set<CampaignMember> memSets = new Set<CampaignMember> (members);

OR 

List<CampaignMember> members = [Select Id, Status From CampaignMember Where Status='Registered'];
Set<CampaignMember> memSets = new Set<CampaignMember> ();

memSets.addAll(members)
Best Answer chosen by Raj R.
Shri RajShri Raj
This should work. 
List<CampaignMember> members = [Select Id, Status From CampaignMember Where Status='Registered'];
Set<CampaignMember> memSets = new Set<CampaignMember> ();

memSets.addAll(members)

All Answers

logontokartiklogontokartik
I am not sure about others, but using the Constructor seems to be closest to best practise. Well. One thing though is you need to check for NULLS everytime you use this set. 

Thank you.
Shri RajShri Raj
This should work. 
List<CampaignMember> members = [Select Id, Status From CampaignMember Where Status='Registered'];
Set<CampaignMember> memSets = new Set<CampaignMember> ();

memSets.addAll(members)
This was selected as the best answer