title: collecting HashMap<String, List<String>> java 8
body: <p>I want to be able to convert a <code>List</code> to a <code>HashMap</code> where the key is the <code>elementName</code> and the values is a list of something random (in this case its the Element Name). So in short I want (<code>A-&gt;List(A), B-&gt;List(B), C-&gt; List(C)</code>). I tried using <code>toMap()</code> and passing it the <code>keyMapper</code> and <code>ValueMapper</code> but I get a compilation error. I would really appreciate if someone can help me out. </p>
  
  <p>Thanks!</p>
  
  <pre><code>public static void main(String[] args) {
      // TODO Auto-generated method stub
      List&lt;String&gt; list = Arrays.asList("A","B","C","D");
      Map&lt;String, List&lt;String&gt;&gt; map = list.stream().map((element)-&gt;{
          Map&lt;String, List&lt;String&gt;&gt; map = new HashMap&lt;&gt;();
          map.put(element, Arrays.asList(element));
          return map;
      }).collect(??);
  }
  
  
  Function&lt;Map&lt;String, String&gt;, String&gt; key = (map) -&gt; {
      return map.keySet().stream().findFirst().get();
  };
  
  Function&lt;Map&lt;String, String&gt;, String&gt; value = (map) -&gt; {
      return map.values().stream().findFirst().get();
  };
  </code></pre>
  
  <p>=== <strong>This worked for me</strong> </p>
  
  <p>Thanks for all the help guys! @izstas "they should operate on the elements" helped a lot :). Actually this is what I was looking for to be exact </p>
  
  <pre><code>public static void test2 (){
      Function&lt;Entry&lt;String, List&lt;String&gt;&gt;, String&gt; key = (entry) -&gt; {
          return entry.getKey();
      };
      Function&lt;Entry&lt;String, List&lt;String&gt;&gt;, List&lt;String&gt;&gt; value = (entry) -&gt; {
          return new ArrayList&lt;String&gt;(entry.getValue());
      };
      BinaryOperator&lt;List&lt;String&gt;&gt; merge = (old, latest)-&gt;{
          old.addAll(latest);
          return old;
      };
  
      Map&lt;String, List&lt;String&gt;&gt; map1 = new HashMap&lt;&gt;();
      map1.put("A", Arrays.asList("A1", "A2"));
      map1.put("B", Arrays.asList("B1"));
      map1.put("D", Arrays.asList("D1"));
  
      Map&lt;String, List&lt;String&gt;&gt; map2 = new HashMap&lt;&gt;();
      map2.put("C", Arrays.asList("C1","C2"));
      map2.put("D", Arrays.asList("D2"));
  
      Stream&lt;Map&lt;String, List&lt;String&gt;&gt;&gt; stream =Stream.of(map1, map2);
      System.out.println(stream.flatMap((map)-&gt;{
          return map.entrySet().stream(); 
      }).collect(Collectors.toMap(key, value, merge)));
  }
  </code></pre>