Generate Reports from Collections
By the end of this lesson, you'll be able to:
- Build a Map<String, Integer> reporting headcount per department
- Use a nested collection to group employees by department
- Explain why generating reports from existing collections avoids a second data pass
Prerequisites: "Deduplicate and Validate (Sets)"
Headcount per department
public Map<String, Integer> getHeadcountByDepartment() {
Map<String, Integer> headcount = new Map<String, Integer>();
for (Employee emp : employeesById.values()) {
String dept = emp.getDepartment();
if (! headcount.containsKey(dept)) {
headcount.put(dept, 0);
}
headcount.put(dept, headcount.get(dept) + 1);
}
return headcount;
}
public Map<String, Integer> getHeadcountByDepartment() {— returns a count per department.Map<String, Integer> headcount = new Map<String, Integer>();— starts empty.- (blank line)
for (Employee emp : employeesById.values()) {— loops over every employee.String dept = emp.getDepartment();— reads their department.if (! headcount.containsKey(dept)) {— the first time a department is seen...headcount.put(dept, 0);— ...its count starts at0.}— closes theif.headcount.put(dept, headcount.get(dept) + 1);— increments the existing count either way.}— closes the loop.- (blank line)
return headcount;— a single pass over every employee produces a complete report, no separate query or loop per department needed.}— closes the method.
Grouping employees by department (nested collections)
public Map<String, List<Employee>> getEmployeesByDepartment() {
Map<String, List<Employee>> grouped = new Map<String, List<Employee>>();
for (Employee emp : employeesById.values()) {
String dept = emp.getDepartment();
if (! grouped.containsKey(dept)) {
grouped.put(dept, new List<Employee>());
}
grouped.get(dept).add(emp);
}
return grouped;
}
public Map<String, List<Employee>> getEmployeesByDepartment() {— this is Module 16's "Nested Collections" lesson — aMap<String, List<Employee>>— put to real use.Map<String, List<Employee>> grouped = new Map<String, List<Employee>>();— starts empty.- (blank line)
for (Employee emp : employeesById.values()) {— loops over every employee.String dept = emp.getDepartment();— reads their department.if (! grouped.containsKey(dept)) {— the first time a department is seen...grouped.put(dept, new List<Employee>());— ...creates an empty list for it.}— closes theif.grouped.get(dept).add(emp);— adds this employee to their department's list.}— closes the loop.- (blank line)
return grouped;— every department's full employee list, generated from the same underlying data as the headcount report above, no re-querying needed.}— closes the method.
Why generate reports from existing data
Both reports loop over employeesById.values() once and build their result in memory — no second database query, no re-fetching data that's already sitting in the directory. This mirrors the bulkification instinct from Module 7: do the necessary work in one clean pass over data you already have, rather than repeating expensive operations per report.
Exercise
Add a method getTotalHeadcount() that returns the sum of every department's count from getHeadcountByDepartment().
Show hint
Loop over headcount.values() and sum them.
Generate Reports from Collections Quiz
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
This lesson generates two useful reports directly from the existing employee data: headcount per department, and a full grouping of employees by department, using nested collections from Module 16.