For example check method in java.util.Collections
public static <T> int binarySearch(List<? extends T> list, T key, Comparator<? super T> c)
Why can I define it this way:
public static <T> int binarySearch(List<T> list, T key, Comparator<T> c)
Why does this will not work in java?
You could define it that way, but then that wouldn't let you search a List<Circle>
using a Comparator<Shape>
, for example.
Basically, the variance being expressed here allow for more flexibility while maintaining type safety.
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