I have two entities, Person
and Age
. While the values of Person
are unique, Age
can be be same for multiple Person
.
If I wanted to only lookup Age
of a Person
, I would have used a HashMap
. But I also want to retrieve a list of Person
of a particular Age
. The solution I can think of is having another HashMap<String, List<Long>>
for the reverse lookup. Is there a data structure or a map like interface with O(1) lookup in both the directions that does the job of two HashMap
's in one?
Please note that I used Person
and Age
as a trivial example, and that the real examples are not something stored in a database, but fetched from a service, so I have to handle them as I get.
Update:
I think Guavas MultiMap can solve this issue. Because in my case both the key and value are String
, so it will work. Just seems a little unclean though.