Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 594
cnova Avatar asked Dec 07 '16 14:12

cnova


People also ask

How to create generic variable in Java?

It specifies the type parameters (also called type variables) T1, T2, ..., and Tn. To update the Box class to use generics, you create a generic type declaration by changing the code "public class Box" to "public class Box<T>". This introduces the type variable, T, that can be used anywhere inside the class.

Can you declare a variable with a generic type?

Yes it is possible, but only for Functions, not any arbitrary variable. As you can see, it's the type itself, where you define generics and then you can make a variable of that type, which allows it to set the generic.

What is generic data type in Java?

Generics means parameterized types. The idea is to allow type (Integer, String, … etc., and user-defined types) to be a parameter to methods, classes, and interfaces. Using Generics, it is possible to create classes that work with different data types.

What does type do in Java?

Type is the common superinterface for all types in the Java programming language. These include raw types, parameterized types, array types, type variables and primitive types.


2 Answers

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".

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( ...
like image 180
Ovidiu Dolha Avatar answered Oct 01 '22 05:10

Ovidiu Dolha


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)
like image 42
Basheer AL-MOMANI Avatar answered Oct 01 '22 05:10

Basheer AL-MOMANI