# How do you directly populate a Map<Id, SObject> from a SOQL query without using a for loop? (new Map<Id, Account>([SELECT Id, Name FROM Account])).

**Topic:** Soql · **Difficulty:** Easy · **Asked at:** Deloitte, Accenture, Infosys · **Tags:** soql, apex, collections

Read it on the site: https://www.forcechallenges.com/interview-questions/how-do-you-directly-populate-a-map-id-sobject-from-a-soql-query-without-using-a-for-loop-new-map-id-account

## Answer

Pass the query directly into the Map constructor. Salesforce uses each record's Id as the key.

```apex
Map<Id, Account> accMap = new Map<Id, Account>([SELECT Id, Name FROM Account WHERE Industry = 'Tech']);
Account a = accMap.get(someId);
```
This avoids manual loops, is concise, and counts as one SOQL query. It works only for keys that are Ids; for other keys use a loop.

---
Source: ForceChallenges · last updated 2026-10-21
