I am new to Java generics and I am so confused.
I wrote this piece of codes, but I got error "Required type: capture of ? extends Number, Provided: Integer".
What went wrong?
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
public class TestGeneric {
// I want to create a list of Functions, and these Functions can have different input/output types than one another.
private List<Function<? extends Number, ? extends Number>> functions;
public TestGeneric() {
functions = new ArrayList<>();
}
public void addFunction(Function<? extends Number, ? extends Number> function) {
functions.add(function);
}
public void main(String[] args) {
TestGeneric testGeneric = new TestGeneric();
Function<Integer, Integer> hundredTimes = new Function<Integer, Integer>() {
@Override
public Integer apply(Integer integer) {
return integer * 100;
}
};
functions.add(hundredTimes);
Function<Double, Double> tenTimes = new Function<Double, Double>() {
@Override
public Double apply(Double o) {
return 10.0 * o;
}
};
functions.add(tenTimes);
System.out.println(functions.get(0).apply(Integer.valueOf(10)));
}
}