+ Start a Discussion
cool 183cool 183 


Can anyone please give me a real time scenario where we use set in apex code to retrieve unique details. As list gives all data including duplicates, and I have used only list in my codings. So I do not want a code but a scenario where set only should be used and not the list..
suresh dupadasuresh dupada

Collections have their own properties and Functionality,If you are not aware of it then, Please  First Understand the Properties and Functionality, 

and Definitely You will get an Idea when to Use them

Please Follow Below URL



Sets can be used to various of purposes here are few
  • remove duplicates (for a example you want to show set of unique values from your query results) eg: unique contact cities
  • to prevent update errors when doing bulk updates , Salesforce returns a error if your update object list contains duplicated records , you can use sets to avoid it .
List : 
  • You can perform DML operations.
  • It is ordered and have duplicates ( As Sura said also in bulk DMLs you can have errors)
Sets :
  • Sets we use actually for the unique values.
  • You cannot perform DML with Sets.
So if you want unique values in a list, put the values in a set and then populate a list from the set.
Use MAP this is my favorite.
For maps go to this link https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections_maps.htm