title: Collect List<Object> to Map<String, List<Object>> in Java 8 using stream
body: <p>I am trying to do this iteration using java 8 stream instead of traditional for loop.</p>
  
  <p>I have a List of Object <code>List&lt;Obligation&gt;</code>
  and the <code>Obligation</code> is having <code>List</code> of <code>String</code> within.</p>
  
  <p><code>Obligation</code> contains fields like </p>
  
  <p><code>f1</code> and <code>f2</code> of type <code>String</code>,  </p>
  
  <p>and </p>
  
  <p><code>f3</code> of type <code>List&lt;String&gt;</code></p>
  
  <p>What I want to achieve is <code>Map&lt;String, List&lt;Obligation&gt;&gt;</code> such that, <code>String</code> used in key of <code>Map</code> is each value present in <code>List&lt;String&gt;</code> from each <code>Obligation</code> within <code>List&lt;Obligation&gt;</code></p>
  
  <p>This is how the code would look in traditional for loop:</p>
  
  <pre><code>// newly initialized Map
  Map&lt;String, List&lt;Obligation&gt;&gt; map = new LinkedHashMap&lt;String, List&lt;Obligation&gt;&gt;();
  
  // assume the list is populated
  List&lt;Obligation&gt; obligations = someMethod();
  
                  for(Obligation obligation : obligations) {
                      for(String license : obligation.getLicenseIDs()) {
                          if(map.containsKey(license)) {
                              map.get(license).add(obligation);
                          } else {
                              List&lt;Obligation&gt; list = new ArrayList&lt;Obligation&gt;();
                              list.add(obligation);
                              map.put(license, list);
                          }
                      }
                  }
  </code></pre>