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?
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.
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.
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.
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.
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( ...
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With