title: Build a Set of Node.Leaf.Id in one line with Java 8
body: <p>I have 2 entities : </p>
  
  <ul>
  <li>Leaves</li>
  <li>Nodes, which contains Leaves.</li>
  </ul>
  
  <p>I have a <code>Collection&lt;Node&gt;</code> and I am trying to build a <code>Set&lt;Integer&gt;</code> of all <code>Leaf</code> ids in one line of code. I feel that it might be possible with <code>Stream</code>s but until now, I can only dot it like this : </p>
  
  <pre><code>Set&lt;Integer&gt; leafIds = Sets.newHashSet();
  root.getNodes()
      .forEach(node -&gt; node.getLeaves()
                .forEach(leaf -&gt; leafIds.add(leaf.getId())));
  </code></pre>
  
  <p>I don't like the part where I manually create the collection and add elements to it with method <code>Collection.add()</code> (not thread safe, dangerous and not optimized). I feel that it might be possible to do something like : </p>
  
  <pre><code>root.getNodes()
    .stream()
    .???
    .getLeaves()
    .map(Leaf::getId)
    .distinct()
    .collect(Collectors.toSet());
  </code></pre>
  
  <p>Any idea?</p>