The wildcard <? super T>
allows you to work with a broader set of types.
Suppose you have some generic function:
Function<Number, String> func = String::valueOf;
Then you could do the following:
List<Integer> list = List.of(1, 2);
Stream<String> stream = list.stream().map(func);
or the following:
List<Long> list = List.of(1L, 2L);
Stream<String> stream = list.stream().map(func);
And this is possible because the parameter to the Stream.map(...)
is:
Function<? super T, ? extends R> mapper;
Which means that if, for example, T
is of type Long
1, then <? super Long>
will allow the function to accept elements of type Long
, and the following assignment will also become valid:
Function<? super Long, ? extends String> mapper = func;
With the Function<T, ? extends R>
both examples above wouldn't even compile.
1 - We construct the Stream<Long>
from the elements of the List<Long>
.