Hi friends, In this post, I will show a sample code that helps you to convert list of queried sobject data to set. By using this example you can collect all id of data without using loop.

List of sobject to Set

In the following example, I have queried account data and collect id of all data in SET without using loop.

Example:

 List<Account> accList =[SELECT Id,Name,AccountNumber FROM Account];
Set<Id> AccSet = new Map<Id,Account>(accList).keySet();
System.debug('AccountIds '+ AccSet);

Output:

 

Hope you like this post, for any feedback or suggestions please feel free to comment. I would appreciate your feedback and suggestions.
Thank you.