Reputation: 13
The below code snippet is from oracle docs: I have a few doubts about the syntax. Need proper and detailed explanation.
Write a generic method to find the maximal element in the range [begin, end) of a list.
public final class Algorithm {
public static <T extends Object & Comparable<? super T>>
T max(List<? extends T> list, int begin, int end) {
T maxElem = list.get(begin);
for (++begin; begin < end; ++begin)
if (maxElem.compareTo(list.get(begin)) < 0)
maxElem = list.get(begin);
return maxElem;
}
}
<T extends Object & Comparable<? super T>>
T
has to extend both Object
and Comparable
. What will be the issue or difference if it were just
<T extends Comparable<? super T>>
?Comparable<? super T>
cannot be just Comparable<T>
?List<T>
instead of List<? extends T>
?Upvotes: 1
Views: 68
Reputation: 271185
For the most part, Object
is redundant here. See this post for more info.
If you constrain it to only T extends Comparable<T>
, that means if I have a Dog
that can be compared to any Animal
(Dog implements Comparable<Animal>
), and I want the method to give me the "maximum" Dog
in a list of Dog
s (I should be able to do this), the method will return an Animal
instead. T
can't be Dog
, since Dog
does not implement Comparable<Dog>
.
Saying Comparable<? super T>
allows T
s that implement Comparable<SomeSuperclassOfT>
to be used.
This is similar to 2. If you make it List<T>
, then I can't give you a List<Dog>
and ask you to return an Animal
(T
can't be Animal
if I pass a List<Dog>
). However, as far as I can tell, this doesn't really affect much in practice, because I can still convert the returned Dog
to an Animal
if I want.
Upvotes: 1