I have some code with 3 overriding methods that perform the same operation ( code is identically for each method) on the Inputs, the only difference between them is the input parameter type
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 was trying to attempt to replace these 3 methods with a single method that makes us of generic wildcards as follows
private List<String> extractNumberValues(Function<MyClass, List<? extends Number>> extractions)
When I attempt to use the above generic method in place of one of the 3 type specific methods
Function<MyClass, List<Integer>> intExtractions;
List<String> extractedValues = extractNumberValues(intExtractions);
I get a the following compilation error on 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've successfully replaced duplicate methods with the wildcard before, as bellow
List<String> convertNumberListToStringList(List<Integer> numberList)
List<String> convertNumberListToStringList(List<Double> numberList)
with
List<String> convertNumberListToStringList(List<? extends Number> numberList)
I'm new to the idea of generics so I was curious as why the above would fail to compile? I do not quite understand why it would fail to compile