Simple example:
Map<String,Object> myMap = ...
Function<Object,String> myFunc = Object::toString;
myMap.computeIfAbsent("42", myFunc);
Is this acceptable?
Well, given the type of myFunc is Function<Object,String>
, and we have a Map<String,Object>
, which sounds like there is a problem. It certainly wouldn't compile if you only accepted Function<K,V>
.
But when you look at it closer, there isn't anything wrong. Not only does the code demonstrably work, it's also easy to see why it works: "42"
is an instance of String
, which has a toString()
method. That method returns a String
, which is of course an Object
. So of course it's fine.
And this "fine"-ness is what is expressed by Function<? super K, ? extends V>
.