Activity Forums Salesforce® Discussions What is Map Class in Apex Salesforce?

  • Hariom Chaudhary

    Member
    September 12, 2019 at 5:16 am

    Hi,

    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.

  • Saddam

    Member
    September 12, 2019 at 6:26 am

    Hi

    A map is a search table that you build from scratch. You choose all the possible search terms, and for each one you add a search result. It’s like building your very own Google search!

    Building and using map goes like this:

    Choose a data type for the search terms and another for the search results of your map.
    – Search terms are usually Strings or IDs.
    – Search results are usually sObjects (like an Account, Opportunity, or Contact).
    Add each search term and search result to your map as a pair.
    “Search” your map with any search term and the search result is returned to you!

     

    // Step 1: The search term (email) data type will be a string
    // The search result (name) data type will also be a string
    Map<String, String> friendMap = new Map<String, String>();
    // Step 2: Add our friends as a search term/result pair
    friendMap.put('[email protected]', 'David Liu');
    friendMap.put('[email protected]', 'Taylor Swift');
    friendMap.put('[email protected]', 'Shakira');
    friendMap.put('[email protected]', 'Marissa Mayer');
    // Step 3: Search our map using the get() function
    String biggestFan = friendMap.get('[email protected]');
    System.assertEquals('Taylor Swift', biggestFan); // TRUE

    • This reply was modified 5 years, 2 months ago by  Saddam.
  • Achintya

    Member
    September 12, 2019 at 8:10 am

    A map is a collection of key-value pairs where each unique key maps to a single value. In map Keys can be any primitive data type like string, integer, decimal, Id while values can be a primitive, sObject, collection type or an Apex object.
    Map keys and values can be of any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types.
    Map keys of type String are case-sensitive. Map methods, including put, get, containsKey, and remove treat these keys as distinct.
    The map gives value on the basis of the key.
    Key always unique but the value can be duplicated.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos