ApexSensei

Drills Apex basics

Count Leads by Rating with a Map

A Map<String, Integer> can keep a running count per key.

Your task

Write countByRating(List<Lead> leads) that counts Leads per Rating in a Map<String, Integer>, treating a null Rating as None, then loop over the keys and print each count.

Map<String, Integer> countByRating(List<Lead> leads) {
    return null;
}

List<Lead> leads = new List<Lead>{
    new Lead(LastName = 'Ito', Company = 'Acme', Rating = 'Hot'),
    new Lead(LastName = 'Park', Company = 'Globex', Rating = 'Warm'),
    new Lead(LastName = 'Diaz', Company = 'Initech', Rating = 'Hot'),
    new Lead(LastName = 'Chen', Company = 'Umbrella')
};
Map<String, Integer> counts = countByRating(leads);

Requirements

  1. for (String key : counts.keySet()) should be used to walk the Map
  2. Hot: 2 should be printed
  3. None: 1 should be printed
  4. countByRating with two Cold Leads should be 2
  5. countByRating without Hot Leads should be null
  6. countByRating with two Rating-less Leads should be 2
  7. countByRating with a hidden mix should be 3

Sign in to open this drill