Best data structure for two way mapping

后端 未结 1 2029
谎友^
谎友^ 2021-01-17 20:00

I want a data structure that maps from key to object and vice-versa(unlike HashMaps that map only in a single direction.) An idea could be to store the HashMap within itself

1条回答
  •  生来不讨喜
    2021-01-17 20:35

    Simplest idea: wrapper class which contains 2 maps, second with swapped keys/values. You will keep O(1) complexity and will use only slightly more memory since you will (probably) keep there reference to object.

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