Data structure for when key and value are equally

2019-08-23 09:22发布

So, this is probably a stupid question, but I have a mapping of unique IDs to unique values. Sometimes I want the value for a certain ID, sometimes I want to know the ID of a certain value. I search more than I modify the collection. I'm wondering if there's a special datastructure that makes sense here, or if I should just maintain two copies of the collection (which is never super large)--one keyed by ID, and one keyed by value.

Thanks!

2条回答
\"骚年 ilove
2楼-- · 2019-08-23 10:16

Something like Google's BiMap.

查看更多
Deceive 欺骗
3楼-- · 2019-08-23 10:20

Maintaining two copies of the collection is the canonical solution.

Note that the two directions can use different collection types if appropriate (e.g. hash tables with different hash functions, a hash table and a balanced tree, etc.).

查看更多
登录 后发表回答