There is class called Foo as following:
public class Foo<E> {
private List<E> ls;
public Foo(List<E> ls) {
this.ls = ls;
}
public void add(E l) {
this.ls.add(l);
}
public <F> Foo<F> map(Function<? super E, ? extends F> f) {
Foo<F> res = new Foo<F>(new ArrayList<F>());
for (E l : ls) {
res.add(f.apply(l));
}
return res;
}
}
Then, I call from main:
//Main.java
Foo<Integer> foo1 = new Foo<>(Arrays.asList(1,2,3,4));
Foo<String> foo2 = foo1.map(a -> a.toString());
I want to see how defining the function as Function<? super E, ? extends F>
gives an advantage over Function<E,F>
? Is Function<? super E, ? extends F>
for has the maximum flexibility? Could you please show it in a example by changing Main.java
?