Efficiently generate Set from List

    sfdcsharepoint.com

    Let’s check with some examples,

      n

    1. Using Map
    2. n

    List results = Database.query('Select Id From Account Limit 5');nSet accIds = (new Map(results)).keyset();

    In second line we are creating new Map from the results of first line, and then taking the keys from this map. Once this done, heap space is released for this map, and so it is one of the efficient method.

      n

    1. One Liner (Without Dynamic SOQL)
    2. n

    Set accIds = (new Map([Select Id from Account Limit 5])).keyset();
      n

    1. One Liner (With Dynamic SOQL)
    2. n

    String sQuery = 'Select Id from Account Limit 5';nSet accIds = (new Map((List)Database.Query(sQuery))).keySet();

    Leave a Reply

    Your email address will not be published. Required fields are marked *