title: Java 8 how to convert List of Map to Map of List?
body: <p>I already followed many links like : <a href="https://www.mkyong.com/java8/java-8-convert-list-to-map/" rel="nofollow noreferrer">https://www.mkyong.com/java8/java-8-convert-list-to-map/</a> and <a href="https://stackoverflow.com/questions/20363719/java-8-listv-into-mapk-v">Java 8 List&lt;V&gt; into Map&lt;K, V&gt;</a>, but I did not come to know how to create <code>Map&lt;Integer, List&lt;Integer&gt;&gt;</code></p>
  
  <p>Assuming the fact that one Employee can work in many department, so I am trying to get that here</p>
  
  <p>Here is my code:</p>
  
  <pre><code>List&lt;Map&lt;String, Object&gt;&gt; results = jdbcTemplate.queryForList(SQL, empId);
  
  Map&lt;Integer, List&lt;Integer&gt;&gt; resultMap = new HashMap&lt;&gt;();
  
  List&lt;Integer&gt; tempLst = new ArrayList&lt;&gt;();
  for (Map&lt;String, Object&gt; map : results) {
      tempLst.add(Integer.valueOf(map.get("DEPARTMENT_ID").toString().trim()));
  }
  resultMap.put(empId,tempLst);
  </code></pre>
  
  <p>I tried something like below:</p>
  
  <pre><code>results.stream().collect(Collectors.toMap(currencyId, Arrays::asList))
  </code></pre>
  
  <p>But it doesn't serve my purpose, I also tried </p>
  
  <pre><code>results.stream().collect(groupingBy(currencyId, mapping(Integer.valueOf(map.get("EMP_ID").toString().trim()), toList())));
  </code></pre>