Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bidirectional multimap equivalent data structure

I know that Guava has a BiMultimap class internally but didn't outsource the code. I need a data structure which is bi-directional, i.e. lookup by key and by value and also accepts duplicates.

i.e. something like this: (in my case, values are unique, but two values can point to the same key)

0 <-> 5
1 <-> 10
2 <-> 7
2 <-> 8
3 <-> 11

I want to be able to get(7) -> returning 2 and get(2) returning [7, 8]. Is there another library out there which has a data structure I can make use of?

If not, what do you suggest is the better option to handle this case? Is keeping two Multimaps in memory one with and the other with a bad practice?

P.S.: I have read this question: Bidirectional multi-valued map in Java but considering it is dated in 2011, I thought I'll open a more recent question

like image 606
Bernice Avatar asked Jul 20 '14 12:07

Bernice


1 Answers

What do you mean by

Guava has a BiMultimap class internally but didn't outsource the code

The code of an implementation is here.

I didn't check if this is a working implementation, nor if it made it into a release or if I'm just looking at some kind of snapshot. Everything is out in the open, so you should be able to get it.

From a quick glance at the source code it looks like the implementation does maintain two MultMaps, and this should be fine for the general case.

like image 94
Jens Schauder Avatar answered Sep 29 '22 03:09

Jens Schauder