Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Salesforce
  3. Question
Salesforce

List vs set - What's the difference?

Asked by Aashna Saito Feb 23, 2023 1.1K views 1 answer
Share

About this question

 I have always assumed that it is better to use a Set in my query filters instead of a List. For example:

Set parentIds = generateParentIds();
List children = [SELECT Id FROM Child__c WHERE Parent__c IN :parentIds];
My reasoning was:

Set can be more easily (and quickly) generated from a Map via keySet().

Set ought to consume less memory than List (no longer sure this is true).

null values can more easily be removed from a Set.

Set cannot contain any duplicate values.

What are the actual benefits to using Set instead of List? What are the drawbacks? What factors help decide? For example I know if the field is nillable, then the ability to remove null is much more important, because it helps avoid a table-scan.

Your answer

1 Answer

More Salesforce discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Salesforce Blogs

Guides, tips and career advice on Salesforce from JanBask experts.