简体   繁体   中英

Best data structure for two way mapping

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 for reverse look-up, but it will be an inefficient approach.

What would be the best implementation for two-way mapping?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM