Consider you want to map a CharSequence
to another CharSequence
(thus T = R = CharSequence
). Which functions will work for you?
Function<Object, String> fn1 = Object::toString;
Is it good for you? Yes, because it can take any CharSequence
(which is also Object
) and convert it to the String
(which is also CharSequence
).
Function<CharSequence, StringBuilder> fn2 = StringBuilder::new;
Is it good for you? Yes, because it can take any CharSequence
and convert it to the StringBuilder
(which is also CharSequence
).
Function<String, String> fn3 = String::trim;
Is it good for you? No, because it cannot take any CharSequence
, only some of them.
Thus you can see that first type argument must be CharSequence
or any superclass, but second must be CharSequence
or any subclass.