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

  1. I always enjoy visiting digitneed because it provides accurate tech updates, useful tools, and detailed reviews, which are perfect for anyone wanting to stay ahead in the ever-changing digital world.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. I am thoroughly impressed with the professionalism of this website, yirafire. From detailed guides to helpful insights, every section offers valuable content. It’s rare to find such dedication to quality and user satisfaction in one place.

    ReplyDelete
  4. Newsforte is truly impressive in its approach to presenting news. The website balances timely updates with detailed analysis, providing readers with valuable insights. I always find the articles reliable, well-written, and a pleasure to read whenever I visit.

    ReplyDelete

Post a Comment

Popular posts from this blog

Salesforce Process Builder in Brief (Learning/Interview)

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

Basics of Omnistudio

Karate Framework

Cloud Computing