如何在Java中使用泛型投射列表?
请考虑以下代码段:
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<MyInterface> getMyInterfaces() {
ArrayList<MyPojo> myPojos = new ArrayList<MyPojo>(0);
myPojos.add(new MyPojo(0));
myPojos.add(new MyPojo(1));
return (ArrayList<MyInterface>) myPojos;
}
return 语句执行不编译的强制转换。如何将myPojos列表转换为更通用的列表,而不必遍历列表的每个项目?
谢谢