I need to have a map between 2 kind of objects, lets say, for demonstrarion purposes, String and Long.
I know a HashMap has an O(1) search performance, which is great. I am using something like this:
HashMap<String, Long> a = new HashMap<String, Long>();
This works great when I want to search by the key. However, now I do need to also find the key searching by value.
If I start iterating over keys, then I will lose the performance.
The first option I have thought about is to create another HashMap the other way around.
HashMap<Long, String> reverseA = new HashMap<Long, String>();
And whenever I write in one, write in the other one.
Isn't there an specific class to be able to search by keys and by values, with that great performance?
Should I create my own class with both HashMaps?
Any other solution?
reverseAmap is an option, then the values better be unique or there will be a bit of inconsistency.