How to transform key/value in Map
<p>I have a Map where key is a String and value is comma seperated list of values.</p>

<p>How can I reverse the ordering of the key/value pairs and assign to new Map so that keys become values and the values become keys</p>

<p>So : </p>

<blockquote>
<pre><code>key   value
key1  value1,value2,value3
key2  value1,value4,value5        
key3  value4,value2,value1
</code></pre>
</blockquote>

<p>becomes : </p>

<blockquote>
<pre><code>key       value
value1    key1,key2,key3
value2    key1,key3
value3    key1
value4    key2,key3
value5    key2
</code></pre>
</blockquote>

<p>A possible solution is to iterate over each value and then iterate of every key
searching for same corresponding value. If found add this new key/value pair to a new Map.
This seems inefficient ?</p>

<p>Solution (implemented using accepted answer) : </p>

<pre><code>import java.util.HashMap;
import java.util.Map;

public class MapTransformer {

    public static void main(String args[]) {

        Map&lt;String, String&gt; m1 = new HashMap&lt;String, String&gt;();

        m1.put("key1", "value1,value2");
        m1.put("key2", "value5");
        m1.put("key3", "value4,value2,value1");

        Map&lt;String, String&gt; inverseMap = new HashMap&lt;String, String&gt;();
        for (Map.Entry&lt;String, String&gt; entry : m1.entrySet()) {
            for (String value : entry.getValue().split(",")) {
                if (inverseMap.containsKey(value))
                    inverseMap.put(value, inverseMap.get(value) + "," + entry.getKey());
                else
                    inverseMap.put(value, entry.getKey());
            }
        }

        for (Map.Entry&lt;String, String&gt; entry : inverseMap.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key+" "+value);
        }
    }

}
</code></pre>
