title: Collect a Stream of objects into a Map
body: <p>I have a csv file of user details which I will have to load into a HashMap by reading each line. This is done using a little help from Java 8 streams.</p>
  
  <pre><code> Map&lt;String,MockUser&gt; users = new HashMap&lt;&gt;();
   try (Stream&lt;String&gt; stream = Files.lines(Paths.get(f.getAbsolutePath()))) {
              stream.forEach(s -&gt; {
                  String[] fields = s.split(",");
                  MockUser user = new MockUser(fields[0], fields[1], fields[2]);
                  users.put(user.getUsername(), user);
              });    
          } catch (IOException e) {
              e.printStackTrace();
          }
  
  
  private class MockUser {
          String username;
          String password;
          String role;
  
          MockUser(String username, String password, String role) {
              this.username = username;
              this.password = password;
              this.role = role;
          }
  
          public String getUsername() {
              return username;
          }
  
          public void setUsername(String username) {
              this.username = username;
          }
  
          public String getPassword() {
              return password;
          }
  
          public void setPassword(String password) {
              this.password = password;
          }
  
          public String getRole() {
              return role;
          }
  
          public void setRole(String role) {
              this.role = role;
          }
      }
  </code></pre>
  
  <p>This works well. However, I had to write a 3 line code to split and add the stream results into a map. I tried using <code>Stream.collect</code> , But I quite cant get this thing right?</p>
  
  <p>Is there a way I can simplify the above code using <code>Stream.collect</code>?</p>