Any implementation of Map, i.e. two keys?

前端 未结 3 1732
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-10 06:58

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

Map2 _employees;

So that I can

_empl         


        
相关标签:
3条回答
  • 2020-12-10 07:12

    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.

    0 讨论(0)
  • 2020-12-10 07:17

    The Spiffy Framework appears to provide exactly what you`re looking for. From the Javadocs:

    A two-dimensional hashmap, is a HashMap that enables you to refer to values via two keys rather than one

    The relevant class is TwoDHashMap. It also provides a ThreeDHashMap.

    0 讨论(0)
  • 2020-12-10 07:18

    My first thought was: the easiest way to do this, I think, would be two maps.

    Map< String, Map< String,Employee> > _employees;
    

    But from what it looks like, you just want to be able to look up an employee by either SSN or ID. What's to stop you then from making two maps, or at worst a class that contains two maps?

    As a clarification, are you looking for a compound key being employees are uniquely identified by the combination of their SSN and ID, but not either one by itself, or are you looking for two different ways of referencing an employee?

    0 讨论(0)
提交回复
热议问题