Efficiently generate Set from List

Let’s check with some examples,

  • Using Map

In second line we are creating new Map<Id,Sobject> 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.

  • One Liner (Without Dynamic SOQL)

  • One Liner (With Dynamic SOQL)