Java 不存在类型变量 T 的实例,因此 List<T> 符合 Integer

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41019400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:04:37  来源:igfitidea点击:

No instance(s) of type variable(s) T exist so that List<T> conforms to Integer

javagenericslambdatype-inference

提问by cnova

In the following code:

在以下代码中:

return new HashSet<>(namedParameterJdbcTemplate.query(
    SOME_SQL_QUERY_STRING,
    parametersMap,
    (resultSet, rowNum) -> resultSet.getBigDecimal("GETID")
));

I'm getting a red line under (resultSet, rowNum) -> resultSet.getBigDecimal("GETID"))and the following error : No instance(s) of type variable(s) T exist so that List<T> conforms to Integer. Can someone please help me and tell why this is happening?

(resultSet, rowNum) -> resultSet.getBigDecimal("GETID"))在下面看到一条红线,并出现以下错误:No instance(s) of type variable(s) T exist so that List<T> conforms to Integer。有人可以帮助我并告诉我为什么会这样吗?

采纳答案by Ovidiu Dolha

The fundamental problem is that a different (unwanted) overloaded version of the "query" method is inferred (based on the code) and the lambda (Function) given as third parameter is not appropriate for this version of "query".

根本问题是推断出“查询”方法的不同(不需要的)重载版本(基于代码),并且作为第三个参数给出的 lambda(函数)不适用于此版本的“查询”。

A way to fix this is to "force" the query function you want by providing the type parameter as such:

解决此问题的一种方法是通过提供类型参数来“强制”您想要的查询功能:

return new HashSet<>(namedParameterJdbcTemplate.<BigDecimal>query( ...

回答by Basheer AL-MOMANI

add an explicit casting to your method call

向您的方法调用添加显式转换

in my case I have

就我而言,我有

<T> Map<String, T> getMap(@NotNull String rootPath, @NotNull Class<T> type)

and I used it like

我用它就像

LinkedHashMap<String,String> x = xmlRegestryFile.getMap("path/to/map/of/string", String.class)

but it failed and gave me that error, so I overcomed this error by adding casting

但它失败了并给了我那个错误,所以我通过添加强制转换来克服这个错误

x = (LinkedHashMap<String, String>) xmlRegestryFile.getMap("my/path", String.class)