原始类型为“ java.lang.Iterable”的成员对“ forEach()”的未经检查的调用

时间:2018-11-05 15:19:12

标签: java spring generics compiler-warnings unchecked

我收到此编译器警告。 这是我的类,使用了接口和方法(省略了其他人员):

public class Controller extends BaseController {

//interface
public interface MyInterface<T extends Iterable<? super T>> {
    List<T> getList();
}

//method call
updateJoinTable(request.getProductGrades().size(), instance::getProductGrades);

//method
private static void updateJoinTable(Integer LastValidElement, MyInterface myInterface) {
    myInterface.getList().subList(LastValidElement, myInterface.getList().size())
          .forEach(i ->myInterface.getList().remove(i));
}
}

forEach的最后一部分引起警告。

现在,起初代码没有:

<T extends Iterable<? super T>>

但是我在SO上看到了很多类似的情况,大多数情况下具有可比性,并且通过阅读解决方案可以理解,问题是我的泛型类型绑定到本身具有类型的类型,但是我没有t提供了一个,所以很原始-然后我将该行添加到界面中,并希望警告消失。但是它仍然在那里。 您有什么想法要摆脱它吗?

1 个答案:

答案 0 :(得分:1)

当前的问题是MyInterface myInterface是原始类型。将其设置为非原始,例如:

private static void updateJoinTable(Integer LastValidElement, MyInterface<?> myInterface) {

此外,您可能要考虑不使用forEach。看来您只是想砍掉列表的尾部:

List<?> list = myInterface.getList();
list.subList(LastValidelement, list.size()).clear();