title: How can I cast a list using generics in Java?
body: <p>Please consider the following snippet:</p>
  
  <pre><code>public interface MyInterface {
  
      public int getId();
  }
  
  public class MyPojo implements MyInterface {
  
      private int id;
  
      public MyPojo(int id) {
          this.id = id;
      }
  
      public int getId() {
          return id;
      }
  
  }
  
  public ArrayList&lt;MyInterface&gt; getMyInterfaces() {
  
      ArrayList&lt;MyPojo&gt; myPojos = new ArrayList&lt;MyPojo&gt;(0);
      myPojos.add(new MyPojo(0));
      myPojos.add(new MyPojo(1));
  
      return (ArrayList&lt;MyInterface&gt;) myPojos;
  }
  </code></pre>
  
  <p>The return statement does a casting that doesn't compile. How can I convert the myPojos list to the more generic list, <strong>without having to go through each item of the list</strong>?</p>
  
  <p>Thanks</p>