title: Converting a Collection in a generic way in Java
body: <p>I am trying to convert a Collection of objects that implement DomainEntity to a Collection of objects that implement DomainEntityDTO. DomainEntity objects provide a method toDTO() to do the conversion. </p>
  
  <p>Here is my code. </p>
  
  <pre><code>public class EntityCollectionConverter&lt;T  extends DomainEntityDTO, Y extends DomainEntity&gt; {
  
      public Collection&lt;T&gt; convert(Collection&lt;Y&gt; collection){
  
          Collection&lt;T&gt; dtoList = new ArrayList&lt;&gt;();
          for (DomainEntity domainObject : collection) {
              DomainEntityDTO dto = domainObject.toDTO();
              dtoList.add(dto); // Compiler: "T cannot be applied to DomainEntityDTO"
          }
          return dtoList;
  
      }
  }
  </code></pre>
  
  <p>The line <code>dtoList.add(dto);</code> doesn't compile, because "T cannot be applied to DomainEntityDTO'.</p>
  
  <p>The interface DomainEntity looks like this:</p>
  
  <pre><code>public interface DomainEntity {
      Long getId();
  
      &lt;T extends DomainEntityDTO&gt; T toDTO();
  }
  </code></pre>
  
  <p>Any idea where I am going wrong?</p>