【问题标题】:Unchecked call to 'forEach()' as a member of raw type 'java.lang.Iterable'未经检查地调用“forEach()”作为原始类型“java.lang.Iterable”的成员
【发布时间】:2018-11-05 15:19:11
【问题描述】:

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

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 提供了一个,所以它是原始的 - 然后我将该行添加到接口并期望警告消失。但它仍然存在。 你有什么想法我应该做些什么来摆脱它?

【问题讨论】:

    标签: java spring generics compiler-warnings unchecked


    【解决方案1】:

    直接的问题是MyInterface myInterface 是一个原始类型。使其成为非原始的,例如:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-22
      • 2021-04-17
      • 1970-01-01
      • 2015-08-29
      • 2019-04-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多