I wish to retrieve values from a Java Key/Value pair (Map with one-to-many mapping) having only one corresponding key without looping over all the values in the Map and turning it into a O(n) complexity.
The Key/Value data structure would be like:
K1 –> V1, V2
K2 –> V1, V2, V3
K3 –> V1
K4 –>
K5 –> V4, V2
Would appreciate if anyone has any recommendations around the same or if this can be made better time-complexity wise.
In google’s guava there is a very nice BiMap class where you can look-up both keys and values (it is backed by two Maps).
http://code.google.com/p/guava-libraries/
EDITED: After clarification. I think my answer does not change much. Guava has also very nice MultiMap interface (and several implementations of it) which you can most likely use for your purpose.