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

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


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 上看到了很多类似的案例,主要是具有可比性,我通过阅读解决方案了解到,这个问题是我的泛型类型绑定到一个本身有类型的类型,但我没有提供一个,所以它是原始的 - 然后我将该行添加到界面并预期警告消失。但它仍然存在。你有什么想法我应该怎么做才能摆脱它?


蝴蝶刀刀
浏览 265回答 1
1回答

慕桂英546537

直接的问题是这MyInterface myInterface是一个原始类型。使它成为非原始的,例如:private static void updateJoinTable(Integer LastValidElement, MyInterface<?> myInterface) {此外,您可能需要考虑不使用forEach. 看起来你只是想砍掉列表的尾部:List<?> list = myInterface.getList();list.subList(LastValidelement, list.size()).clear();
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java