Why does map
take its input as a Function
of type <? super P_OUT, ? extends R>
instead of <P_OUT, ? extends R>
?
When I do for example,
List<Integer> list = new ArrayList<>();
list.stream().map(xyz -> {}); // Here xyz is always of type Integer for this instance.
// Then why does it take input as "? super Integer"?
Is it because of not restricting method references? Is that the only use case?
map. Returns a stream consisting of the results of applying the given function to the elements of this stream. This is an intermediate operation.
Java 8 Stream's map method is intermediate operation and consumes single element forom input Stream and produces single element to output Stream. It simply used to convert Stream of one type to another.
map(Function mapper) takes a function as an argument. For example, by using the map() function, you can convert a list of String into a List of Integer by applying the Integer. valueOf() method to each String on the input list. All you need is a mapping function to convert one object to the other.
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>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With