I have the following class.
class MyClass<T>
It uses the following constructor.
MyClass(Comparator<T> comparator, Collection<? extends T> data)
And it has a field which is set in the constructor like so:
this.data = Collections.unmodifiableCollection(data);
In the special case where T implements Comparable, I don't want to require that a comparator be passed in, since I can just use the natural ordering. So I thought I should be able to use this constructor:
public <T extends Comparable<T>> MyClass(Collection<T> data)
But there is apparently a type mismatch: cannot convert from Collection<T> to Collection<? extends T>
in the assignment statement above. I've tried all sorts of things: adding more generic parameters, and so on, but none work. I seem unable to specify a bound that says: if you have a type T that implements Comparable, do the straightforward thing.
Any ideas?
Thanks.