dev
dev

Reputation: 2584

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?

Upvotes: 12

Views: 6174

Answers (1)

AbstractProblemFactory
AbstractProblemFactory

Reputation: 9811

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.

Upvotes: 11

Related Questions