Need a bidirectional Map which allows duplicate values, and returns list of values given a key
<p>I have a need for a data structure which I think should be a common need<br>
I need a Map which allows duplicate values(most commonly do, but wait.. getting to the point).. see this sample<br></p>

<pre><code>k1 -&gt; v1
k2 -&gt; v1
k3 -&gt; v1
k4 -&gt; v2
k5 -&gt; v2
</code></pre>

<p>Now if I do map.getByValue(v1), I should get Set(k1, k2, k3) . Otherwise it should behave like a 'normal' map. It should have high performance, so no for-loop kind of suggestions please.<br>
Also, note that the following will not do for me<br></p>

<pre><code>v1 -&gt; (k1, k2, k3)
v2 -&gt; (k4, k5)
</code></pre>

<p>.. since I dont want this situation ever (note, k1 is in both lists)..</p>

<pre><code>v1 -&gt; (k1, k2, k3)
v2 -&gt; (k4, k5, k1)
</code></pre>

<p>I don't want to use two map solution. I am thinking of some way to keep values sorted so that getByValue(value) is still performant.</p>

<p>High performance means that you need some sort of index to search for a key. There are no indexes that work in both directions, so actually you need two of them. So you have to use two multimaps, one for each direction, and a wrapper that will maintain them in consistent state.</p>

java c# python data-structures