如何在Java中使用泛型来转换列表?

Ry.*_*Ry. 26 java generics casting

请考虑以下代码段:

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;
}
Run Code Online (Sandbox Code Playgroud)

return语句执行不编译的转换.如何将myPojos列表转换为更通用的列表,而不必遍历列表中的每个项目

谢谢

Jon*_*eet 41

更改方法以使用通配符:

public ArrayList<? extends MyInterface> getMyInterfaces() {    
    ArrayList<MyPojo> myPojos = new ArrayList<MyPojo>(0);
    myPojos.add(new MyPojo(0));
    myPojos.add(new MyPojo(1));

    return myPojos;
}
Run Code Online (Sandbox Code Playgroud)

这将阻止调用者尝试将该接口的其他实现添加到列表中.或者,你可以写:

public ArrayList<MyInterface> getMyInterfaces() {
    // Note the change here
    ArrayList<MyInterface> myPojos = new ArrayList<MyInterface>(0);
    myPojos.add(new MyPojo(0));
    myPojos.add(new MyPojo(1));

    return myPojos;
}
Run Code Online (Sandbox Code Playgroud)

正如评论中所讨论的:

  • 返回通配符集合对于调用者来说可能很尴尬
  • 通常最好使用接口而不是返回类型的具体类型.所以建议的签名可能是以下之一:

    public List<MyInterface> getMyInterfaces()
    public Collection<MyInterface> getMyInterfaces()
    public Iterable<MyInterface> getMyInterfaces()
    
    Run Code Online (Sandbox Code Playgroud)


Pet*_*rey 24

从一开始就选择正确的类型是最好的,但是要回答你的问题,你可以使用类型擦除.

return (ArrayList<MyInterface>) (ArrayList) myPojos;

  • IMO,这应该是答案,因为在某些情况下,你根本无法将项添加到基类型的集合中:从查询中想到JPA结果集,你有一个JPA实体列表,如果你想返回这个列表给调用者使用实体上面的一些抽象接口(持久性不可知),Peter的建议是*the*way (3认同)

Jac*_*oen 5

你应该这样做:

public ArrayList<MyInterface> getMyInterfaces() {   
   ArrayList<MyInterface> myPojos = new ArrayList<MyInterface>(0);    
   myPojos.add(new MyPojo(0));    
   myPojos.add(new MyPojo(1));    
   return myPojos;
}
Run Code Online (Sandbox Code Playgroud)