class User{
private int id;
private String name;
public User(int id, String name) {
this.id = id;
this.name = name;
}
}
class Service<T> {
private List<T> data;
public void setData(List<T> data) {
this.data = data;
}
}
public class ServiceTest {
public static void main(String[] args) {
Service<User> result=new Service<User>();
result.setData(Collections.emptyList()); // problem is here
}
}
如何通過類型參數傳遞空列表?如何通過類型參數傳遞空列表?
編譯器給我的錯誤信息:
The method setData(List< User >) in the type Service is not applicable for the arguments (List< Object >)
,如果我試圖用列表來投那麼錯誤:
Cannot cast from List< Object > to List< User >
result.setData(new ArrayList<User>());
工作正常,但我不想把它傳遞。
它適用於Java的8 – ZhongYu