title: Make a map of Department to List of employee from employee object containing list of departments
body: <p>Question: Employee contains list of departments and we have a list of employees now how to obtain a map like Map&lt;Department,List of Employees&gt; from employee list.</p>
  <p>The below code does the trick but I wanted to know how to use streams api effectively instead of for loop.</p>
  <pre><code>    Department a = new Department(&quot;a&quot;);
      Department b = new Department(&quot;b&quot;);
      Department c = new Department(&quot;c&quot;);
  
      Employee e1 = new Employee(&quot;e1&quot;, List.of(a, b));
      Employee e2 = new Employee(&quot;e2&quot;, List.of(c, b));
      Employee e3 = new Employee(&quot;e3&quot;, List.of(c, a));
      Employee e4 = new Employee(&quot;e4&quot;, List.of(a, b, c));
  
      List&lt;Employee&gt; employees = List.of(e1, e2, e3, e4);
      Set&lt;Department&gt; departments = employees.stream().flatMap(employee -&gt;
              employee.getDepartments().stream()).collect(Collectors.toSet());
  
      for (Department d : departments) {
          for (Employee employee : employees) {
              if (employee.getDepartments().contains(d)) {
                  if (!result.containsKey(d)) {
                      result.put(d, new ArrayList&lt;Employee&gt;());
                  }
                  result.get(d).add(employee);
              }
          }
      }
      return result;
  </code></pre>