HashMap manipulation using streams Java 8
<p>Please let me know if there is a possibility of changing the below code in terms of Java 8 using parallel streams?</p>

<p>I am looking for an option to run the "outer for loop" in parallel and finally all the values of stationMap gets collected together?</p>

<pre><code>Map&lt;Integer, Set&lt;Integer&gt;&gt; stationMap = new HashMap&lt;&gt;();
Map&lt;Integer, Set&lt;Integer&gt;&gt; routes = function();
for (Map.Entry&lt;Integer, Set&lt;Integer&gt;&gt; entry : routes.entrySet()) 
{
     Set&lt;Integer&gt; stations = entry.getValue();

      for (Integer station : stations) {
        Set&lt;Integer&gt; temporaryStations = new HashSet&lt;&gt;(stations);
        Set&lt;Integer&gt; stationSet = stationMap.get(station);
        if (stationSet == null) {
          stationSet = new HashSet&lt;&gt;();
          temporaryStations.remove(station);
          stationSet.addAll(temporaryStations);
          stationMap.put(station, stationSet);
        } else {
          temporaryStations.remove(station);
          stationSet.addAll(temporaryStations);
        }
      }
    }
</code></pre>

<p>More shorter version:</p>

<pre><code>routes.forEach((k, stations) -&gt; {
      stations.forEach((station) -&gt; {
        Set&lt;Integer&gt; stationSet = stationMap.get(station);
        if (stationSet == null) {
          stationSet = new HashSet&lt;&gt;();
          stationSet.addAll(stations);
          stationMap.put(station, stationSet);
        } else {
          stationSet.addAll(stations);
        }
      });
    });
</code></pre>