Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any implementation of Map<K1, K2, V>, i.e. two keys?

I need a map that has two keys, e.g.

Map2<String /*ssn*/, String /*empId*/, Employee> _employees;

So that I can

_employees.put(e.ssn(), e.empId(), e)

And later

_employees.get1(someSsn);
_employees.get2(someImpId);

Or even

_employees.remove1(someImpId);

I am not sure why I want to stop at two, why not more, probably because that's the case I am I need right now :-) But the type needs to handle fixed number of keys to be type-safe -- type parameters cannot be vararg :-)

Appreciate any pointers, or advice on why this is a bad idea.

like image 308
Miserable Variable Avatar asked Nov 22 '08 08:11

Miserable Variable


1 Answers

I imagine the main key would be empId, so I would build a Map with that as the key, i.e. empId ---> Employee. All other unique attributes (e.g. ssn) would be treated as secondary and will use separate Maps as a lookup table for empId (e.g. ssn ---> empId).

This implementation makes it easy to add/remove employees, since you only need to change one Map, i.e. empId ---> Employee; the other Maps can be rebuilt only when needed.

like image 90
Zach Scrivena Avatar answered Nov 03 '22 00:11

Zach Scrivena