ApexSensei

Drills Apex basics

Maps: Id to Account

A Map<Id, Account> stores each Account under its Id.

Your task

Build an Id-to-Account Map from a List, look up a known key and a missing key, and loop over every key.

Id cedarId = Id.valueOf('001000000000001');
Id northstarId = Id.valueOf('001000000000002');
Id missingId = Id.valueOf('001000000000099');
List<Account> accountList = new List<Account>{
    new Account(Id = cedarId, Name = 'Cedar Labs'),
    new Account(Id = northstarId, Name = 'Northstar Co')
};
Map<Id, Account> accountsById = null;

Requirements

  1. Fetched account: Northstar Co should be printed
  2. Has missing key: false should be printed
  3. Missing value: null should be printed
  4. Loop name: Cedar Labs should be printed
  5. Loop name: Northstar Co should be printed
  6. new Map<Id, Account>(accountList) should be used
  7. for-each loop should be used over accountsById.keySet()

Sign in to open this drill