The is using the principle of Producer Extends Consumer Super (PECS).
Basically, PECS says that if a generic parameter is used as a parameter type of a method, it should use contravariance i.e. super
. If it is used as a return value type, it should use covariance i.e. extends
.
U
is constrained to extend Comparable<? super U>
. Here, super
is used to achieve contravariance for U
. So even if U
is String
, you can pass in a Comparable<Object>
.
The Function
parameter type is Function<? super T,? extends U>
to achieve contravariance on T
(consumer, as is used in parameter type of Function
) and covariance on U
(as is used in return value type of Function
). This enables you to pass a Function<Object, String>
even if T
is String
and U
is Object
.
The significance is that without extends
or super
, this is called invariance. This means that you have to pass instances of exactly the generic parameter types T
and U
, which reduces flexibility.
To learn more about PECS, visit here.