public class ApexCollectionConversion{
/******************** FOR LIST **********************************************/
public void convert_List_INTO_Set(){
// Conver List into Set
List<String> lstEnglishCapitalLetters = new List<String> {'A', 'B'};
// Method 1:
Set<String> setEnglishCapitalLetters = new Set<String>(lstEnglishCapitalLetters);
// Method 2:
setEnglishCapitalLetters.addAll(lstEnglishCapitalLetters);
// Method 3:
for(String engLetter : lstEnglishCapitalLetters){
setEnglishCapitalLetters.add(engLetter);
}
}
public void convert_List_INTO_Map(){
// Conver List into Map
// Method 1:
List<String> lstEnglishSmallLetters = new List<String> {'a', 'b'};
Map<String, String> mapEnglishSmallLetters = new Map<String, String>();
for(String engLetter : lstEnglishSmallLetters){
mapEnglishSmallLetters.put(engLetter, engLetter);
}
// Method 2:
List<Account> lstAccounts = [SELECT Id, Name FROM Account WHERE Name = 'Salesforce'];
Map<Id, Account> mapAccounts = new Map<Id, Account>(lstAccounts);
}
/******************** FOR SET **********************************************/
public void convert_Set_INTO_List(){
// Conver Set into List
Set<String> setFruitsName = new Set<String>{'Apple', 'Orange'};
// Method 1:
List<String> lstFruitsName = new List<String>(setFruitsName);
// Method 2:
lstFruitsName.addAll(setFruitsName);
}
public void convert_Set_INTO_Map(){
// Conver Set into Map
Set<String> setVagetablesName = new Set<String>{'Potato', 'Carrot'};
Map<String, String> mapVagetablesName = new Map<String, String>();
for(String vegetable : setVagetablesName){
mapVagetablesName.put(vegetable, vegetable);
}
}
/******************** FOR MAP **********************************************/
public void convert_Map_INTO_List(){
// Conver Map into List
Map<String, String> mapCountryAbbreviations = new Map<String, String>{'USA' => 'United States of America', 'UK' => 'United Kingdom'};
List<String> lstCountryShortName = new List<String>(mapCountryAbbreviations.keySet());
List<String> lstCountryFullName = new List<String>(mapCountryAbbreviations.values());
}
public void convert_Map_INTO_Set(){
// Conver Map into Set
Map<String, String> mapCityCodes = new Map<String, String>{'1001' => 'California', '1002' => 'London'};
Set<String> setCountryShortName = new Set<String>(mapCityCodes.keySet());
Set<String> setCountryFullName = new Set<String>(mapCityCodes.values());
}
}
The On Demand Model: Multi-tenancy & Subscription:
The benefit of coding in the CLOUD is that you learn how to write more efficient code and waste fewer resources.
Pages
- Home
- Basics of Salesforce.com
- Basics of Force.com
- New To Salesforce (For Administrators / Functional Track)
- New To Salesforce (For Developers / Technical Track)
- Salesforce Advanced Administrator [ADM 301]
- Salesforce Advanced Developer [DEV 501]
- Salesforce Certified Platform App Builder
- Salesforce Certified Platform Developer
- Salesforce Certified Application & System Architect
- About Me