title: Java generics and streams
body: <p>I'm building a sort of framework to avoid repetition of code, and at a certain point I need to convert a list of object Foo into a list of object Bar.</p>
  
  <p>I have database entities that extend</p>
  
  <pre><code>public class BaseEntity {...}
  </code></pre>
  
  <p>And presentation objects that extend</p>
  
  <pre><code>public class BaseDTO&lt;T extends BaseEntity&gt; {...}
  </code></pre>
  
  <p>so</p>
  
  <pre><code>public class Foo extends BaseEntity {...}
  </code></pre>
  
  <p>and</p>
  
  <pre><code>public class Bar extends BaseDTO&lt;A extends BaseEntity&gt; {
      public Bar(Foo entity, Locale l) {
          ...
      }
  }
  </code></pre>
  
  <p>Now to convert a list of Foo into a list of Bar is easy using streams</p>
  
  <pre><code>public abstract ClassThatUsesFooAndBar() {
  
      public List&lt;Bar&gt; convertFooToBar(List&lt;Foo&gt; list) {
          return list.stream().map(f -&gt; new Bar(f, locale)).collect(...);
      }
  }
  </code></pre>
  
  <p>But, and here is the question, these Foo and Bar are actually generics (A and B), so the class that uses Foo and Bar actually is <code>ClassThatUsesAandB&lt;A extends BaseEntity, B extends BaseDTO&gt;</code>, so that function must be abstract too and implemented as boilerplate code with the correct A and B implementations because obviously you cannot instantiate generic types.</p>
  
  <p>Is there a way to use generics/streams/lambdas to create a function that can be written once, so that the implementing classes don't need to re-implement it? The function signature would be</p>
  
  <pre><code>public List&lt;B&gt; convertAToB(List&lt;A&gt; list);
  </code></pre>
  
  <p>I hope I've been clear enough in what I need, if you need further explanations please ask</p>
  
  <p>Thank you!</p>