DIFFERENCE BETWEEN SET, LIST AND MAP

Three different types of collections in Apex.
  1. List
  2. Set
  3. Map
  • List: List simply called collection of duplicate values and which contains Integer indexed data.
    • Create new LIST.
      • List<String> myList = new List<String>();
    • Add into LIST.
      • myList.add(‘Navin’);
      • myList = new List<String>{ ‘Navin’, ‘Aryan’ };
    • Duplicate means?
      • myList.add(‘Navin’);
      • myList.add(‘Aryan’);
      • myList.add(‘Navin’);
      • Here the size of LIST is 3. We can get this by myList .size()
      • At index 0, myList[0] = we get Navin
      • At index 1, myList[1] = we get Aryan
      • At index 2, myList[2] = we get Navin
  • Set: Set simply called collection of Unique values and cannot be sort.
    • Create new SET.
      • Set<String> mySet = new Set<String>();
    • Add into Set.
      • mySet .add(‘Navin’);
      • mySet = new mySet <String>{ ‘Navin’, ‘Aryan’ };
    • Unique means?
      • mySet.add(‘Navin’);
      • mySet.add(‘Aryan’);
      • mySet.add(‘Navin’);
      • Here the size of SET is 2. We can get this by mySet .size()
      • We can check “if(mySet .contains(‘Navin’)){ system.debug(‘mySet=>’+ mySet.size())}”. Which shows answer is “mySet=>2″
  • Map: Maps are collections of key-value pairs, where the keys is Unique.
    • Create new Map.
      • Map<Integer,String> employeeAddresses = new Map<Integer,String>();
      • Integer is KEY and it is unique.
      • String is VALUE.
      • Example : School Bag(Unique) with same and different book and copy(Not Unique).
    • Add into Map.
      • employeeAddresses.put (1, ‘123 Sunny Drive, San Francisco, CA’)
    •  Unique means?
      • employeeAddresses.put (1, ‘456 Dark Drive, San Francisco, CA’);
      • employeeAddresses.put (2, ‘456 Dark Drive, San Francisco, CA’);
      • employeeAddresses.put (1, ‘222 White Drive, San Francisco, CA’);
      • Here the size of MAP is 2. We can get this by employeeAddresses.size()
      • We can check “if(employeeAddresses .containsKey(‘1′)){ system.debug(’employeeAddresses =>’+ employeeAddresses .get(‘1’))}”. Which shows answer is “employeeAddresses =>222 White Drive, San Francisco, CA”
      • Here we can see 1 that is key and can override with last value i.e. “222 White Drive, San Francisco, CA” not “456 Dark Drive, San Francisco, CA”.

Comments

Popular posts from this blog

Service Cloud for Lightning Experience - All Steps Q&A [Salesforce][Trailhead]

Salesforce Process Builder in Brief (Learning/Interview)

Cloud Computing

Copado Winter 21 Administrator Maintenance Dumps

Einstein Analytics Data Preparation Specialist