Flatten a Map<Integer, List<String>> to Map<String, Integer> with stream and lambda
<p>I would like to flatten a <code>Map</code> which associates an <code>Integer</code> key to a list of <code>String</code>, without losing the key mapping.
I am curious as though it is possible and useful to do so with <code>stream</code> and <code>lambda</code>.</p>

<p>We start with something like this:</p>

<pre><code>Map&lt;Integer, List&lt;String&gt;&gt; mapFrom = new HashMap&lt;&gt;();
</code></pre>

<p>Let's assume that mapFrom is populated somewhere, and looks like:</p>

<pre><code>1: a,b,c
2: d,e,f
etc.
</code></pre>

<p>Let's also assume that the values in the lists are unique.</p>

<p>Now, I want to "unfold" it to get a second map like:</p>

<pre><code>a: 1
b: 1
c: 1
d: 2
e: 2
f: 2
etc.
</code></pre>

<p>I could do it like this (or very similarly, using <code>foreach</code>):</p>

<pre><code>Map&lt;String, Integer&gt; mapTo = new HashMap&lt;&gt;();
for (Map.Entry&lt;Integer, List&lt;String&gt;&gt; entry: mapFrom.entrySet()) {
    for (String s: entry.getValue()) {
        mapTo.put(s, entry.getKey());
    }
}
</code></pre>

<p>Now let's assume that I want to use lambda instead of nested <code>for</code> loops. I would probably do something like this:</p>

<pre><code>Map&lt;String, Integer&gt; mapTo = mapFrom.entrySet().stream().map(e -&gt; {
    e.getValue().stream().?
    // Here I can iterate on each List, 
    // but my best try would only give me a flat map for each key, 
    // that I wouldn't know how to flatten.
}).collect(Collectors.toMap(/*A String value*/,/*An Integer key*/))
</code></pre>

<p>I also gave a try to <code>flatMap</code>, but I don't think that it is the right way to go, because although it helps me get rid of the dimensionality issue, I lose the key in the process.</p>

<p>In a nutshell, my two questions are :</p>

<ul>
<li>Is it possible to use <code>streams</code> and <code>lambda</code> to achieve this?</li>
<li>Is is useful (performance, readability) to do so?</li>
</ul>