function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Naveen Sirivella 8Naveen Sirivella 8 

Which purposes we are using list,set and Map

Hi Guys,

Can any one explain me which purposes we are using list,set and Map with each one one real time scenarios.please
Amit Chaudhary 8Amit Chaudhary 8
Please check below post for same
1) http://amitsalesforce.blogspot.com/2016/09/collection-in-salesforce-example-using.html

List, Set, Map are called collections in Apex:
List : A list is an ordered collection
1) so use list when you want to identify list element based on Index Number.
2) list can contain Duplicates

EX: List<Account> accList = new List<Account>();

Set : A set is an unordered collection
1) Do not contain any duplicate elements. So, use set if you want to make sure that your collection should not contain Duplicates.

EX: Set<Account> accSet = new Set<Account>()

Set<String> setString = new Set<String>(); // Add two strings to it
setString .add('item1');
setString .add('item2');

Map : A map is a collection of key-value pairs 
Each unique key maps to a single value. Keys can be any primitive data type, while values can be a primitive, sObject, collection type or an Apex object.

EX: Map<Id, Account> accMap = new Map<Id, Account>();

Map<Integer, String> mapOfString = new Map<Integer, String>();
mapOfString.put(1, 'Amit');
mapOfString.put(2, 'Rahul');
System.assert(mapOfString.containsKey(1)); 
String value = mapOfString.get(2); 
System.assertEquals('Rahul', value); Set<Integer> s = mapOfString.keySet();
Map<String, String> myMap = new Map<String, String>{'a' => 'b', 'c' => 'd'};

Let us know if this will help you
 
raj_sfdccraj_sfdcc
Hello Naveen ,

Collections are very much used when  you are dealing with bulk records and complex logics.

List collection Type : 
List is collection of similar datatypes stored in the single variable .

We can assign the values to the list dynamically. List allows duplicate values to stored.

Map collection Type : 

Map is key value pair which contains unique key for each value .


Please use the below post to learn more about Collections(LIst Vs Map Vs Set) with real time examples :
Usage of collections in Salesforce (https://salessforcehacks.blogspot.com/2020/01/collections-in-salesforce-list-set-map.html)