Java 8 Stream - operations on objects with same id within Collection
<p>My question is a bit complex (I think), so I'll try to use an example. Hopefuly you'll understand my problem.</p>

<p>Set's say I have a List of users:</p>

<pre><code>List&lt;User&gt; users;
</code></pre>

<p>Where:</p>

<pre><code>public class User{
    private List&lt;Car&gt; cars;
}

public class Car{
    private String code;
    private List&lt;Feature&gt; features;
}

public class Feature{
    private String code;
}
</code></pre>

<p>What I want to achieve is get the list of Cars from the list of Users BUT I also want this list to be distinct (no cars with the same code) BUT I want the single Car object to have all of the Features from different cars with the same code and that's where I fail.</p>

<p>For now I have</p>

<pre><code>List&lt;Car&gt; cars = users
        .stream()
        .flatMap(user -&gt; user.getCars())
        .???
</code></pre>

<p>I know I can filter cars by code using Predicate but I'm suffering to add features to the ones I'm leaving. Is there a clean way to do it with streams? </p>