2

I have just started learning java generics.

I have encountered the following question and answer online

Question:

Write a generic method to find the maximal element in the range [begin, end) of a list.

Answer:

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;
    }
}

However, I was just wondering what is the purpose of <T extends Object & Comparable<? super T>>? If I instead used <T extends Comparable<T>>, wouldn't the code be a lot easier to understand and still work as expected?

Could someone please enlighten me? Thanks in advance for any help!

Thor
  • 323
  • 1
  • 4
  • 10

0 Answers0