If the empId
cannot conflict with a SSN (a big assumption I'm making), then you don't really need two Maps. Just do two inserts for each employee. (Maps store references, not objects, so this won't take a lot more memory.) If the empId
does conflict with the SSN format, you can always change the type parameter of the Map Key to Object and create a wrapper class for your keys.
Map<String /*ssn or empId*/, Employee> _employees;
_employees.put(e.ssn(), e);
_employees.put(e.empId(), e);
This just maps two keys to the same employee. Consequently, you should get the same Employee object from the following two method calls.
_employees.get(someSsn);
_employees.get(someImpId);
The best part is, if you need to add more keys later you can. Just make sure they don't conflict with the format of SSN or empId
(or wrap them, as I suggested earlier). The worst drawback that I can see to this method is that you'd need to do two remove
operations when you want to delete an employee. So, for example, if you want to remove an employee with a certain empId
, you'd have to get
that employee, then remove
them based on all the keys in your Map.
SSNs are always nine decimal digits (in the U.S.). If your employee ID is any other format, then you can use this solution.