Compilation error when passing Integer to a generic method using the <T ,? expands the number >>

I have a code with 3 overriding methods that perform the same operation (the code is the same for each method) on the inputs, the only difference between them is the type of input parameter

private List<String> extractDoubleValues(Function<MyClass, List<Double>> extractions)
private List<String> extractLongValues(Function<MyClass List<Long>> extractions)
private List<String> extractIntegerValues(Function<MyClass, List<Integer>> extractions)

I tried to try replacing these 3 methods with one method that makes us universal wildcards as follows

private List<String> extractNumberValues(Function<MyClass, List<? extends Number>> extractions)

When I try to use the above general method instead of one of the three types of specific methods

Function<MyClass, List<Integer>> intExtractions; 
List<String> extractedValues = extractNumberValues(intExtractions);

I get the following compilation error in the second line of code above

Error:(59, 80) java: incompatible types: java.util.function.Function<MyClass,java.util.List<java.lang.Double>> cannot be converted to java.util.function.Function<MyClass,java.util.List<? extends java.lang.Number>>

I have successfully replaced duplicate methods with a wildcard earlier, as below

List<String> convertNumberListToStringList(List<Integer> numberList)
List<String> convertNumberListToStringList(List<Double> numberList)

with

List<String> convertNumberListToStringList(List<? extends Number> numberList)

, , ? ,

+4
2

( , ?) , . . , List<Integer> List<Double> . Fucntion, <T, R> . List<Integer> List<Double> . , R - List, :

Function<SomeClass, ? extends List<? extends SomeOtherClass>>

, R List , SomeOtherClass.

generic , generic ...

0

Function<MyClass, List<Number>> Function<MyClass, List<Integer>>, , List, .. Long Double List<Integer>. , .

+1

Source: https://habr.com/ru/post/1690378/


All Articles