Intermediate 30 min read

Model an Employee Directory (Maps)

By the end of this lesson, you'll be able to:

  • Write the Employee wrapper class from the design sketch
  • Write an EmployeeDirectory class holding a Map<String, Employee>
  • Add and look up employees by employee ID

Prerequisites: "Design the Data Structures"

The Employee wrapper class

public class Employee {
    private String employeeId;
    private String name;
    private String department;

    public Employee(String employeeId, String name, String department) {
        this.employeeId = employeeId;
        this.name = name;
        this.department = department;
    }

    public String getEmployeeId() { return employeeId; }
    public String getName() { return name; }
    public String getDepartment() { return department; }
}
  1. public class Employee { — this is Module 16's wrapper class pattern combined with Module 10's encapsulation.
  2. private String employeeId; — private fields, set once through the constructor.
  3. private String name; — another protected field.
  4. private String department; — another.
  5. (blank line)
  6. public Employee(String employeeId, String name, String department) { — the constructor.
  7. this.employeeId = employeeId; — sets the id.
  8. this.name = name; — sets the name.
  9. this.department = department; — sets the department.
  10. } — closes the constructor.
  11. (blank line)
  12. public String getEmployeeId() { return employeeId; } — a controlled getter.
  13. public String getName() { return name; } — another.
  14. public String getDepartment() { return department; } — another — all three fields exposed through controlled getters.
  15. } — closes the class.

EmployeeDirectory backed by a Map

public class EmployeeDirectory {
    private Map<String, Employee> employeesById = new Map<String, Employee>();

    public void addEmployee(Employee emp) {
        employeesById.put(emp.getEmployeeId(), emp);
    }

    public Employee getEmployee(String employeeId) {
        return employeesById.get(employeeId);
    }

    public Integer getEmployeeCount() {
        return employeesById.size();
    }
}
  1. public class EmployeeDirectory { — the class.
  2. private Map<String, Employee> employeesById = new Map<String, Employee>(); — this is exactly Module 16's Map<Id, sObject> pattern, applied to a custom Employee type instead of a real Salesforce record.
  3. (blank line)
  4. public void addEmployee(Employee emp) { — adds an employee.
  5. employeesById.put(emp.getEmployeeId(), emp); — one put() per employee, keyed by their id.
  6. } — closes the method.
  7. (blank line)
  8. public Employee getEmployee(String employeeId) { — looks one up.
  9. return employeesById.get(employeeId); — instant, regardless of how many employees the directory holds.
  10. } — closes the method.
  11. (blank line)
  12. public Integer getEmployeeCount() { — reports the total.
  13. return employeesById.size(); — returns it.
  14. } — closes the method.
  15. } — closes the class.

Putting it together

EmployeeDirectory directory = new EmployeeDirectory();

directory.addEmployee(new Employee('E001', 'Amara Nkosi', 'Engineering'));
directory.addEmployee(new Employee('E002', 'Ben Dlamini', 'Sales'));

Employee found = directory.getEmployee('E001');
System.debug(found.getName()); // "Amara Nkosi"
System.debug(directory.getEmployeeCount()); // 2
  1. EmployeeDirectory directory = new EmployeeDirectory(); — creates the directory.
  2. (blank line)
  3. directory.addEmployee(new Employee('E001', 'Amara Nkosi', 'Engineering')); — adds the first employee.
  4. directory.addEmployee(new Employee('E002', 'Ben Dlamini', 'Sales')); — adds a second.
  5. (blank line)
  6. Employee found = directory.getEmployee('E001'); — no looping required to find E001 — the Map handles the lookup directly.
  7. System.debug(found.getName()); — logs "Amara Nkosi".
  8. System.debug(directory.getEmployeeCount()); — logs 2, exactly the performance benefit highlighted back in Module 16's "Maps" lesson.

Exercise

Add a removeEmployee(String employeeId) method to EmployeeDirectory that removes an employee from the map.

Show hint

employeesById.remove(employeeId);

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

Model an Employee Directory (Maps) Quiz

1. Why does EmployeeDirectory use a Map instead of a List to store employees?

Log in to submit the quiz and save your score.

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 turns the sketch from Lesson 1 into real classes — an Employee wrapper class, and an EmployeeDirectory backed by a Map for instant lookups by ID.