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
Bhupathi YadavBhupathi Yadav 

what is use of collections?

Iam new to customization, I want to know In  which situation we can go with List,set and map.
 
Anil kumar GorantalaAnil kumar Gorantala
lList,Set,Map are called collections in Apex:

List : A list is an ordered collection
so use list when you want to identify list element based on Index Number.(Lsit can contain Duplicates)
EX: List<account>

Set: A set is an unordered collection of primitives or sObjects that 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>

Map: A map is a collection of key-value pairs where 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> holds id and account record with that id


Find below link for Detailed explanation regarding collections
http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_collections.htm
Anuj Joshi 16Anuj Joshi 16
Hi,

List, Set and map are collections used in apex. These are useful when governor limits are reached to a limit while querying records inside the for loop. Instaed of writing query and reaching the limit we can use set to get ID's of the records and iterate through it.

Map:  It is a collection which stores key,value pairs where key is unique and it can be of any data type like sobject,integer,string etc.

Set: It is a unique collection of values which does not contain duplicates.

List: It is a collection of records which is an ordered collection. The main  diff between list and set is list contains duplicate values but set doesn't contain any duplicate values.


Regards,
Anuj
swetha s 13swetha s 13
Collections work somewhat like arrays, except their size can change dynamically, and they have more advanced behaviors and easier access methods than arrays.
Data collections are simply groupings of any data type. You’ll use data collections often because they go hand in hand with SOQL.
The reason why Lists are so important is because the output of every SOQL query is a List. The unique aspect of Lists is that they are ordered – had we ordered our SOQL query by Email address, this structure would be preserved in our variable! I would like to share  a video link https://www.youtube.com/watch?v=ksshmekZ6V8 which will benefit you.
Set: A set is a collection of unique, unordered elements. It can contain primitive data types (String, Integer, Date, etc) or objects. If you need ordered elements use a List instead. You typically see Sets used to store collections of IDs that you want to use in a SOQL query.
Map: It is a collection of key-value pairs. Keys can be any primitive data type while values can include primitives, Apex objects, objects and other collections. Use a map when you want to quickly find something by a key. Each key must be unique but you can have duplicate values in your Map.
 

 
Anilkumar KotaAnilkumar Kota

Hello 

List : 
You can perform DML operations.
It is ordered and have duplicates 
Sets :
Sets we use actually for the unique values.
You cannot perform DML with Sets.

Map
A map is a collection of key-value pairs where each unique key maps to a single value. Keys and values can be any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types. For example, the following table represents a map of countries and currencies

Ajay K DubediAjay K Dubedi
Hi Bhupathi,
The collection is the type variables which can store multiple numbers of records. It can increase and decrease dynamically.
There are 3 types of collection:
a.List Collection:
List can contain any number of records of primitive, collections, sObjects, user defined and built in Apex type. This is one of the most important type of collection and also, it has some system methods which have been tailored specifically to use with List. List index always starts with 0. This is synonymous to the array in Java. A list should be declared with the keyword 'List'.
Example->
Below is the list which contains the List of a primitive data type (string), that is the list of cities.
List<string> ListOfCities = new List<string>();
System.debug('Value Of ListOfCities'+ListOfCities);
b.Set Collection:
A Set is a collection type which contains multiple number of unordered unique records. A Set cannot have duplicate records. Like Lists, Sets can be nested.
Example->
We will be defining the set of products which company is selling.
Set<string> ProductSet = new Set<string>{'Phenol', 'Benzene', 'H2SO4'};
System.debug('Value of ProductSet'+ProductSet);
c.Map Collection.
It is a key value pair which contains the unique key for each value. Both key and value can be of any data type.
Example->
The following example represents the map of the Product Name with the Product code.
Map<string, string> ProductCodeToProductName = new Map<string, string>
{'1000'=>'HCL', '1001'=>'H2SO4'};
System.debug('value of ProductCodeToProductName'+ProductCodeToProductName);

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
 
raj_sfdccraj_sfdcc
Hi  Bhupathi Yadav,

Collections are very useful when your dealing with bulk records

Below code helps to avoid duplicate email in the object during inserstion or Upgrade .

Collections plays vital role in the below code ,Which makes developer an easy way to write code .
 
The below Trigger helps to ignore duplicate emails in the Author object .

trigger DemoTrigger2 on Author__c (before insert,before update) {
    List<Author__c> accList=new List<Author__c>([select Author_Email__c from Author__c]);
    map<String,Author__c> accmap=new map<String,Author__c>();
    for(Author__c acc:accList){
        accmap.put(acc.Author_Email__c,acc);
    }
    
    if(Trigger.isinsert&&Trigger.isbefore){
        for(Author__c acc:Trigger.new){
            if(accmap.get(acc.Author_Email__c)!=null){
                acc.adderror('Email already exists');
            }
        }
    }   
}

Please use the below post for Detailed exaplanation with usage of collection.

Collections with scenario explanation (https://salessforcehacks.blogspot.com/2020/01/collections-in-salesforce-list-set-map.html)

Let us know if you require any further details .
 
sachinarorasfsachinarorasf
Hi Bhupathi,

Collections is a type of variable that can store multiple numbers of records. For example, a List can store multiple numbers of Account object's records. 

Lists
The list can contain any number of records of primitive, collections, sObjects, user-defined and built-in Apex type. This is one of the most important types of collection and also, it has some system methods which have been tailored specifically to use with List. The list index always starts with 0. This is synonymous with the array in Java. A list should be declared with the keyword 'List'.

Sets
A Set is a collection type that contains multiple numbers of unordered unique records. A Set cannot have duplicate records. Like Lists, Sets can be nested.

Maps
It is a key-value pair that contains the unique key for each value. Both keys and values can be of any data type.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
www.sachinsf.com